简体   繁体   English

如何在 Visual Studio 2010 中并行化数据驱动的单元测试?

[英]How to parallelize a Data-Driven unit test in Visual Studio 2010?

I know regular MS-Test unit tests can be parallelized on a multi-core machine (with caveats of course) by specifying parallelTestCount attribute in the .testresults file in the test solution.我知道通过在测试解决方案的.testresults文件中指定parallelTestCount属性,可以在多核机器上并行化常规的 MS-Test 单元测试(当然有警告)。 Like this,像这样,

<Execution parallelTestCount="1">
    <TestTypeSpecific />
    <AgentRule name="Execution Agents"></AgentRule>
</Execution>

More at: http://blogs.msdn.com/b/vstsqualitytools/archive/2009/12/01/executing-unit-tests-in-parallel-on-a-multi-cpu-core-machine.aspx更多信息: http : //blogs.msdn.com/b/vstsqualitytools/archive/2009/12/01/executing-unit-tests-in-parallel-on-a-multi-cpu-core-machine.aspx

However, I have a data-driven test , something like this, this is just one test, but the input comes in from a csv and runs 1000s of records through the same test.但是,我有一个数据驱动的测试,就像这样,这只是一个测试,但输入来自 csv 并通过同一个测试运行 1000 条记录

[DeploymentItem("InputDataRows.csv"), Timeout(37800000), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\InputDataRow.csv", "InputDataRow#csv", DataAccessMethod.Sequential)]                
[TestMethod]
public void RunProcessing()
{
    int userId = Convert.ToInt32(TestContext.DataRow[0].ToString());
    int connId = Convert.ToInt32(TestContext.DataRow[1].ToString());
    string xml = TestHelper.GetDataFromDb(userId, connId);
    a = doStuffA(xml); 
    b = doStuffB(xml);
    Assert.IsTrue(a == b);
}

Because this is a slow process, I am looking at parallelizing this unit test.因为这是一个缓慢的过程,我正在考虑并行化这个单元测试。

The Sequential enum on the attribute is just the way it accesses data, the other option is Random, which is still serial and not parallel.属性上的Sequential 枚举只是它访问数据的方式,另一个选项是 Random,它仍然是串行而不是并行。

In order to parallelize this unit test, you'll need doStuffA() and doStuffB() to be able to operate on a subset of the data (eg a chunk or even a single row of your csv at a time).为了并行化这个单元测试,你需要doStuffA()doStuffB()才能对数据的一个子集进行操作(例如一次一个块甚至一行你的 csv)。 If you can refactor your methods to behave in this way, you can utilize tasks or a parallel foreach loop so that this test executes in parallel.如果您可以重构您的方法以这种方式运行,您可以利用任务或并行 foreach 循环,以便此测试并行执行。 Suppose your methods were refactored to handle a row of your csv, you could do something like this:假设您的方法被重构以处理一行 csv,您可以执行以下操作:

int userId = Convert.ToInt32(TestContext.DataRow[0].ToString());
int connId = Convert.ToInt32(TestContext.DataRow[1].ToString());
string xml = TestHelper.GetDataFromDb(userId, connId);
var rows = xml.Split('\n');

Parallel.ForEach(rows, (row) =>
{
    var a = doStuffOnRowA(row);
    var b = doStuffOnRowB(row);
    Assert.AreEqual(a, b);
});

This might seem a bit complex, but hear me out.这可能看起来有点复杂,但请听我说。 There is a limitation in MSTest that you cannot actually run data-driven tests in parallel. MSTest 中存在一个限制,即您实际上无法并行运行数据驱动的测试。 What I have done in the past to get around this is to create a "custom tool" in visual studio.我过去为解决这个问题所做的是在 Visual Studio 中创建一个“自定义工具”。

https://msdn.microsoft.com/en-us/library/bb166508.aspx https://msdn.microsoft.com/en-us/library/bb166508.aspx

OR

https://msdn.microsoft.com/en-us/library/bb166817.aspx https://msdn.microsoft.com/en-us/library/bb166817.aspx

The custom tool that we created did the following:我们创建的自定义工具执行以下操作:

  1. Split out the csv into multiple csv files with only one row each.将 csv 拆分为多个 csv 文件,每个文件只有一行。
  2. Generate an individual test for each of the newly generated csvs.为每个新生成的 csv 生成单独的测试。

When these tests were generated, we put specific test attributes on them, so we could specify to only run the tests with that attribute.生成这些测试时,我们将特定的测试属性放在它们上面,因此我们可以指定仅运行具有该属性的测试。

This sounds kind of over the top, but if you do a good job building the custom tool, it's actually a very smooth process.这听起来有点夸张,但是如果您很好地构建了自定义工具,这实际上是一个非常顺利的过程。

If there is no data being changed in the xml (Or the methods are not modifying the same parts of the xml) then you could do..如果 xml 中没有数据被更改(或者方法没有修改 xml 的相同部分),那么你可以做..

var numCompleted = 0;
var a = Task.Run(() => { doStuffOnRowA(xml); });
var b = Task.Run(() => { doStuffOnRowB(xml); });
Task.WaitAll(new Task[2] { a, b });

This might not run if you copy and paste, kind of pseudo code.如果您复制和粘贴这种伪代码,这可能不会运行。 Not exactly Parallel, but will at least run around the same time!不完全平行,但至少会在同一时间运行!

Parallel execution of data driven tests is not supported.不支持数据驱动测试的并行执行。 Please see here: RFC 004 - In-assembly Parallel Execution请参阅此处: RFC 004 - 组装内并行执行

As as I know: individual data rows in a test are NOT run in parallel.据我所知:测试中的单个数据行不是并行运行的。 But if you have multiple unit tests, they do run in parallel.但是如果你有多个单元测试,它们会并行运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在数据驱动的单元测试中指定数据源? - How to specify datasource in data-driven unit test? MSTest 中的通用数据驱动单元测试 - Generic data-driven unit test in MSTest 如何使用C#在.NET中的数据驱动单元测试中实现JSON文件中的测试数据 - How to implement test data in JSON file in data-driven unit test in .NET using C# 在数据源中串联字符串以进行数据驱动的单元测试 - Concatenating strings in DataSource for data-driven unit test 如何停止数据驱动的编码ui测试 - How can I stop a data-driven coded ui test Visual Studio 2010:单元测试:调试器未启动 - Visual Studio 2010: Unit Test: Debugger not starting 数据驱动的单元测试 - CSV编码问题? - Data-driven unit testing - Problem with CSV encoding? 有没有更好的方法将动态输入内联传递给 DataTestMethod? 即如何以编程方式为数据驱动测试创建测试输入 - Is there a better way to pass dynamic inputs in-line to a DataTestMethod? I.e. How to programmatically create test inputs for a data-driven test NUnit中的数据驱动测试? - Data-driven testing in NUnit? 在Visual Studio 2010 Ultimate中没有出现“创建单元测试项目” - “create a unit test project” doesnt appear in visual studio 2010 ultimate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM