Duplicate the behaviour of a data driven test

时光总嘲笑我的痴心妄想 提交于 2020-01-04 15:31:14

问题


Right now, if you have a test that looks like this:

[TestMethod]
[DeploymentItem("DataSource.csv")]
[DataSource(
    Microsoft.VisualStudio.TestTools.DataSource.CSV, 
    "DataSource.csv", 
    "DataSource#csv", 
    DataAccessMethod.Sequential)]
public void TestSomething()
{
    string data = TestContext.DataRow["ColumnHeader"].ToString();
    /* 
    do something with the data
    */
}

You'll get as many tests runs as you have data values when you execute this test.

What I'd like to do is duplicate this kind of behaviour in code while still having a datasource. For instance: let's say that I want to run this test against multiple deployed versions of a web service (this is a functional test, so nothing is being mocked - ie. it could very well be a codedui test against a web site deployed to multiple hosts).

[TestMethod]
[DeploymentItem("DataSource.csv")]
[DataSource(
    Microsoft.VisualStudio.TestTools.DataSource.CSV, 
    "DataSource.csv", 
    "DataSource#csv", 
    DataAccessMethod.Sequential)]
public void TestSomething()
{
    var svc = helper.GetService(/* external file - NOT a datasource */);
    string data = TestContext.DataRow["ColumnHeader"].ToString();
    /* 
    do something with the data
    */
}

Now, if I have 2 deployment locations listed in the external file, and 2 values in the datasource for the testmethod, I should get 4 tests.

You might be asking why I don't just add the values to the datasource. The data in the external file will be pulled in via the deployment items in the .testsettings for the test run, because they can and will be defined differently for each person running the tests and I don't want to force a rebuild of the test code in order to run the tests, or explode the number of data files for tests. Each test might/should be able to specify which locations it would like to test against (the types are known at compile-time, not the physical locations).

Likewise, creating a test for each deployment location isn't possible because the deployment locations can and will be dynamic in location, and in quantity.

Can anyone point me to some info that might help me solve this problem of mine?


回答1:


UPDATE! This works for Visual Studio 2010 but does not seem to work on 2012 and 2013.

I had a similar problem where I had a bunch of files I wanted to use as test data in a data driven test. I solved it by generating a CSV file before executing the data driven test. The generation occurs in a static method decorated with the ClassInitialize attribute.

I guess you could basically do something similar and merge your current data source with your "external file" and output a new CSV data source that your data driven test use.

public TestContext TestContext { get; set; }
const string NameColumn = "NAME";
const string BaseResourceName = "MyAssembly.UnitTests.Regression.Source";

[ClassInitialize()]
public static void Initialize(TestContext context)
{
    var path = Path.Combine(context.TestDeploymentDir, "TestCases.csv");
    using (var writer = new StreamWriter(path, false))
    {
        // Write column headers
        writer.WriteLine(NameColumn);

        string[] resourceNames = typeof(RegressionTests).Assembly.GetManifestResourceNames();
        foreach (string resourceName in resourceNames)
        {
            if (resourceName.StartsWith(BaseResourceName))
            {
                writer.WriteLine(resourceName);
            }
        }
    }
}

[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\TestCases.csv", "TestCases#csv", DataAccessMethod.Random)]
public void RegressionTest()
{
    var resourceName = TestContext.DataRow[NameColumn].ToString();
    // Get testdata from resource and perform test.
}


来源:https://stackoverflow.com/questions/6943403/duplicate-the-behaviour-of-a-data-driven-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!