复制数据驱动测试的行为

发布于 2024-11-28 04:33:36 字数 1322 浏览 4 评论 0原文

现在,如果您有一个如下所示的测试:

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

执行此测试时,您将获得与数据值一样多的测试运行。

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

[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 个测试。

您可能会问为什么我不直接将值添加到数据源中。外部文件中的数据将通过 .testsettings 中的部署项提取以进行测试运行,因为它们可以并且将为运行测试的每个人进行不同的定义,我不希望强制重建测试代码以运行测试,或分解用于测试的数据文件的数量。每个测试可能/应该能够指定要测试的位置(类型在编译时已知,而不是物理位置)。

同样,为每个部署位置创建测试也是不可能的,因为部署位置的位置和数量可以并且将会是动态的。

谁能向我指出一些可以帮助我解决这个问题的信息?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云胡 2024-12-05 04:33:36

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

我遇到了类似的问题,我有一堆文件想在数据驱动测试中用作测试数据。我通过在执行数据驱动测试之前生成 CSV 文件来解决这个问题。生成发生在用 ClassInitialize 属性修饰的静态方法中。

我想您基本上可以做类似的事情,将当前数据源与“外部文件”合并,并输出数据驱动测试使用的新 CSV 数据源。

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

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文