复制数据驱动测试的行为 [英] Duplicate the behaviour of a data driven test

查看:65
本文介绍了复制数据驱动测试的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,如果您进行的测试如下:

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.

我想做的是在仍然有数据源的同时,在代码中复制这种行为.例如:假设我要针对Web服务的多个部署版本运行此测试(这是功能测试,因此没有被嘲笑-即,它很可能是针对部署到多个Web服务站点的codedui测试.主机).

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
    */
}

现在,如果我在外部文件中列出了2个部署位置,并且在测试方法的数据源中有2个值,那么我应该获得4个测试.

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.

您可能会问为什么我不只是将这些值添加到数据源中.外部文件中的数据将通过.testsettings中的部署项引入以进行测试,因为对于运行测试的每个人,它们都可以并且将以不同的方式定义,因此我不想强制重新构建测试代码以运行测试,或爆炸用于测试的数据文件数量.每个测试都可能/应该能够指定要针对哪个位置进行测试(类型在编译时是已知的,而不是物理位置).

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?

推荐答案

更新!这适用于Visual Studio 2010,但似乎不适用于2012和2013.

我有一个类似的问题,我有很多文件要用作数据驱动测试中的测试数据.我通过在执行数据驱动测试之前生成CSV文件来解决该问题.生成以装饰有ClassInitialize属性的静态方法进行.

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.

我想您基本上可以执行类似的操作,并将当前数据源与外部文件"合并,并输出供数据驱动测试使用的新CSV数据源.

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.
}

这篇关于复制数据驱动测试的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆