C# Visual Studio WebTestPlugin在调用ReloadDataTable时失败

C# Visual Studio WebTestPlugin在调用ReloadDataTable时失败,c#,visual-studio-2013,load-testing,web-testing,C#,Visual Studio 2013,Load Testing,Web Testing,在Visual Studio 2013 WebTestPlugin中,我试图在WebTestPlugin PreWebTest事件中更改数据源的连接字符串,然后对数据源中的每个表调用WebTest.ReloadDataTable public void PreWebTest(object sender, PreWebTestEventArgs e) { //fetch the connection string I want to use from the Con

在Visual Studio 2013 WebTestPlugin中,我试图在WebTestPlugin PreWebTest事件中更改数据源的连接字符串,然后对数据源中的每个表调用WebTest.ReloadDataTable

    public void PreWebTest(object sender, PreWebTestEventArgs e)
    {
        //fetch the connection string I want to use from the ContextParameter
        string connectionString = e.WebTest.Context["ConnectionString"].ToString();

        foreach (var ds in e.WebTest.DataSources)
        {

            if(ds.Connection.ToLower() != connectionString.ToLower())
            try
            {
        //point to the Datasource to my desired connection string
                ds.SetConnection("System.Data.SQLClient", connectionString);
                foreach (var table in ds.Tables)
                {
                    table.SelectColumns = DataBindingSelectColumns.SelectAllColumns;
                    //Visual Studio bug--the ReloadDataTable call runs on this datasource and table in this test, and then assumes the same datasource and table exists in every other test and tries to execute there, throwing an exception.
                    e.WebTest.ReloadDataTable(ds.Name, table.Name);
                }
            }
            catch (Exception ex)
            {
                System.IO.File.AppendAllText(LogPath, string.Format("Error Web Test: {0}, ConnectionString {1} {2} {3}", e.WebTest.Name, connectionString, ex.ToString(), Environment.NewLine));
            }
        }
    }
调用WebTest.ReloadDataTable时得到意外结果。有什么办法可以让这一切顺利进行吗

将Database类型的数据源添加到WebTest时,必须为每个数据源静态设置连接字符串。在我的解决方案中,我使用指向开发数据库的数据库连接字符串创建了每个数据源。我有3个运行设置(Dev、QA和Staging)。每个运行设置都有一个“ConnectionString”ContextParameter,它指向自己的数据库

这是我的插件的代码。它从运行设置的ContextParameter中获取连接字符串,将数据源设置为此连接字符串,然后重新加载数据源中的数据表

    public void PreWebTest(object sender, PreWebTestEventArgs e)
    {
        //fetch the connection string I want to use from the ContextParameter
        string connectionString = e.WebTest.Context["ConnectionString"].ToString();

        foreach (var ds in e.WebTest.DataSources)
        {

            if(ds.Connection.ToLower() != connectionString.ToLower())
            try
            {
        //point to the Datasource to my desired connection string
                ds.SetConnection("System.Data.SQLClient", connectionString);
                foreach (var table in ds.Tables)
                {
                    table.SelectColumns = DataBindingSelectColumns.SelectAllColumns;
                    //Visual Studio bug--the ReloadDataTable call runs on this datasource and table in this test, and then assumes the same datasource and table exists in every other test and tries to execute there, throwing an exception.
                    e.WebTest.ReloadDataTable(ds.Name, table.Name);
                }
            }
            catch (Exception ex)
            {
                System.IO.File.AppendAllText(LogPath, string.Format("Error Web Test: {0}, ConnectionString {1} {2} {3}", e.WebTest.Name, connectionString, ex.ToString(), Environment.NewLine));
            }
        }
    }
问题是,对e.WebTest.ReloadDataTable的调用在每个WebTest中都会执行(不仅仅是它所包含的WebTest)。即使我为每个WebTest创建单独的插件,这种情况也会发生

例如,在我的负载测试解决方案中,我有两个web测试:

  • WebTestDog,带有数据源Dogs、表GoodDogs和BadDogs
  • WebTestCat,带有数据源Cats、表GoodCats和BadCats
  • 我有两个使用上述PreWebTest方法的web测试插件:

  • WebTestDogPlugin,添加到WebTestDog测试中
  • WebTestCat插件,添加到WebTestCat测试中
  • 当WebTestDog运行并调用PreWebTest时,将执行此调用:

    e.WebTest.ReloadDataTable("Dogs", "GoodDogs")
    
    并立即抛出异常,并显示以下错误消息:

    Microsoft.VisualStudio.TestTools.WebTesting.WebTestException: 重新加载DataTable失败:Web测试中的数据源“Dogs” “WebTestCat”不包含名为“GoodDogs”的表

    当我在“WebTestDog”的PreWebTest事件中时,为什么Visual Studio在“WebTestCat”中执行此调用


    我可以想出一个解决办法,将数据源“Dogs”添加到“WebTestCat”中。但是为什么我必须这样做呢?在我的实际解决方案中,它有多个测试,这会增加很多混乱,并且会让后面的任何人感到困惑。

    解决方法是每个web测试都有一个相同的“DataSources”节点,并且数据源中的每个表都必须为Select Columns设置“Select all Columns”。这个变通方法增加了混乱和无用的开销,但它是有效的。更正,上面提到的变通方法不起作用。它运行时不会引发异常,但表不会重新加载来自新连接字符串的数据,而是保留使用创建时使用的连接字符串加载的数据。显然,“ReloadDataTable”方法对web测试使用的表数据没有任何作用。另请参见此处讨论的一个非常类似的问题。谢谢@AdrianHHH。我希望它适用。我验证了我正在调用(并且只调用)所需测试中的ReloadDataTable。问题发生在保存ReloadDataTable方法的黑盒中,它决定在每个web测试中执行自己。但如果我绕过这个问题,将所有数据源添加到所有测试中,数据表仍然不会刷新。非常令人沮丧。我放弃了。我的“变通方法”是使用3组不同的测试,除了连接字符串之外,它们是彼此的精确副本。