Freigeben über


VSTS 2010 Feature: Data Source Enhancements

This blog post will describe the enhancements that have been made to the data source functionality in web tests.  We have heard the following feedback a number of times:

  1. I would like to be able to reload the data source while a load test is running
  2. I want to be able to use a column in my data source that is not bound to any parameter in my web test
  3. I want more control over how the data source is advanced.

We have provided enhancements to each of these areas.

  1. Reloading a data source – Prior to this change, all data for the data source was read once at the beginning of the test.  Now this will allow you to programmatically reload a data source while a test is running.  So if you have a set of web tests that are creating data that other tests need, you will now be able to pull that data in while the test is running.  The way to accomplish this is by using a WebTestPlugin.  The API call is the following:  void WebTest.ReloadDataTable(string dataSourceName, string dataTableName)   Here is a very simple plug-in that would call this.  I have a csv data source called DataSource1 and the table to Products#csv.  

    using Microsoft.VisualStudio.TestTools.WebTesting;

    namespace TestProject4
    {
        public class DatasourcePlugin : WebTestPlugin
        {
            public override void PreWebTest(object sender, PreWebTestEventArgs e)
            {
                e.WebTest.ReloadDataTable("DataSource1", "Products#csv");
            }
        }
    }

  2. Using a column in data source not bound to a parameter – This is useful if you are binding some parameter to a column but then want to use another column in a plug-in or custom validation rule.  Prior to this change, you would have to bind all columns that you were going to use to some parameter in the web test.  Now you just do the following. 

    • Expand the data sources node in the web test
    • Click on the table that you want all the data from
    • Bring up the property browser
    • Change the Select Columns property from “Only Select Bound Columns “ to ”Select All Columns”.   This will then add all of the columns into the context and make them available for use in your custom plug-ins and rules.
  3. Controlling how data source is advanced – In VSTS 2008 and VSTS 2005 we had 3 access methods for data sources: Sequential, Random and Unique.  Check out this blog post for a better description of these: Access Methods  We have now added “Do Not Move Cursor Automatically”  Basically, you now have full control on when the cursor is advanced.  If you do nothing, the cursor stays on the first row.  To move the cursor, you will use the following API call: void WebTest.MoveDatatableCursor(string dataSourceName, string dataTableName).  We have also added another overload of this method which is void WebTest.MoveDatatableCursor(string dataSourceName, string dataTableName, int newRowIndex).  Here is a very simple plug-in which will move the cursor to row 3 in the data source. 

    using Microsoft.VisualStudio.TestTools.WebTesting;

    namespace TestProject4
    {
        public class DatasourcePlugin : WebTestPlugin
        {
            public override void PreWebTest(object sender, PreWebTestEventArgs e)
            {
                e.WebTest.MoveDataTableCursor("DataSource1", "Products#csv", 3);
            }
        }
    }

    Hopefully these enhancements will make it easier to work with data sources.  These are now available in beta 2.  Please try them out and let us know what you think.

Comments

  • Anonymous
    October 26, 2010
    Can you pls also let us know how to assign a value from datasource to a context parameter in a Plugin and a WebTest Thanks

  • Anonymous
    June 12, 2012
    You can get the current datasource value used during that ietration like this: e.WebContext.Context["DataSource1.FileName#csv.TableName"] Here FileName is the name of the file where data is stoted and TableName is the name of the table. You can just do this to get the list of all the keys: e.WebContext.Context.Keys One of the keys is the one I mentioned above.

  • Anonymous
    January 25, 2016
    In Visual Studio 2013, I cannot figure out how to get this method to work without creating a separate WebTestPlugin for every WebTest. I have a load test with multiple web tests.  I have my database connection string in a RunSetting ContextParameter, and I created different run settings for each environment (Dev, QA, Staging).  When the PreWebTest fires I set the DataSource.Connection for all datasources in the test to the value stored in Context Parameter, but then when I call WebTest.ReloadDataTable on each table in the DataSource I get an exception, because visual studio attempts to find the DataSource and Table name in a tests where they do not exist. For example, my load test contains WebTestDog and WebTestCat.  WebTestDog contains a Dogs datasource with two tables, "BadDogs" and "GoodDogs."  In my WebTestPlugin, PreWebTest event, when it has been invoked by WebTestDog, I call the following: e.WebTest.ReloadDataTable("Dogs", "BadDogs"); and Visual Studio throws the error message ReloadDataTable failed: The data source 'Dogs' in Web test 'WebTestCat' does not contain a table with the name 'BadDogs' I have verified that the PreWebTest event is being invoked by WebTestDog (I'm logging e.WebTest.Name).  e.WebTest.ReloadDataTable() is assuming that the call must be repeated in all other tests that include the web test plugin. Do I need to create a different WebTestPlugin for each WebTest?

  • Anonymous
    January 26, 2016
    The method WebTest.ReloadDataTable() seems to have no effect on the table of data used by the web test when the table is filled from a connection string.  Is this method only applicable to CSV data sources? stackoverflow.com/.../visual-studio-webtestplugin-fails-in-calls-to-reloaddatatable