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

发布于 2024-09-29 05:16:18 字数 1443 浏览 1 评论 0原文

我知道常规的 MS-Test 单元测试可以通过在测试解决方案的 .testresults 文件中指定parallelTestCount 属性在多核机器上并行化(当然有注意事项) 。像这样,

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

更多信息请参见 MSDN:在多CPU/核心机器上并行执行单元测试

但是,我有一个数据驱动的测试,类似这样的,这只是一个测试,但输入来自 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);
}

因为这是一个缓慢的过程,所以我正在考虑并行化此单元测试。

Sequential enum 只是它访问数据的方式,另一个选项是 Random,即仍然是串行而不是并行。

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. Like this,

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

More at MSDN: Executing Unit Tests in parallel on a multi-CPU/core machine

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.

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

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

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

发布评论

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

评论(5

和我恋爱吧 2024-10-06 05:16:18

不支持数据驱动测试的并行执行。请参阅此处: RFC 004 - 汇编内并行执行

Parallel execution of data driven tests is not supported. Please see here: RFC 004 - In-assembly Parallel Execution

风流物 2024-10-06 05:16:18

为了并行化此单元测试,您需要 doStuffA()doStuffB() 才能对数据子集(例如块甚至数据)进行操作一次 csv 的一行)。如果您可以重构方法以实现这种方式,则可以利用任务或并行 foreach 循环,以便该测试并行执行。假设你的方法被重构来处理你的 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);
});

In order to parallelize this unit test, you'll need doStuffA() and doStuffB() to be able to operate on a subset of the data (e.g. a chunk or even a single row of your csv at a time). 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. Suppose your methods were refactored to handle a row of your csv, you could do something like this:

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);
});
想挽留 2024-10-06 05:16:18

这可能看起来有点复杂,但请听我说完。
MSTest 存在一个限制,即您实际上无法并行运行数据驱动测试。
我过去为解决这个问题所做的就是在 Visual Studio 中创建一个“自定义工具”。

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

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

我们创建的自定义工具执行以下操作:

  1. 将 csv 拆分为多个 csv 文件,每个文件仅一行。
  2. 为每个新生成的 csv 生成单独的测试。

当生成这些测试时,我们在它们上添加特定的测试属性,因此我们可以指定仅运行具有该属性的测试。

这听起来有点夸张,但如果您很好地构建了自定义工具,那么这实际上是一个非常顺利的过程。

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.
What I have done in the past to get around this is to create a "custom tool" in visual studio.

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

OR

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.
  2. Generate an individual test for each of the newly generated csvs.

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.

听风念你 2024-10-06 05:16:18

如果 xml 中没有数据被更改(或者方法没有修改 xml 的相同部分),那么您可以这样做。

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

如果您复制并粘贴某种伪代码,这可能无法运行。
不完全并行,但至少会在同一时间运行!

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

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!

吾家有女初长成 2024-10-06 05:16:18

据我所知:测试中的各个数据行不是并行运行的。但如果您有多个单元测试,它们确实会并行运行。

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.

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