如何在 Visual Studio 2010 中并行化数据驱动单元测试?
我知道常规的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不支持数据驱动测试的并行执行。请参阅此处: RFC 004 - 汇编内并行执行
Parallel execution of data driven tests is not supported. Please see here: RFC 004 - In-assembly Parallel Execution
为了并行化此单元测试,您需要
doStuffA()
和doStuffB()
才能对数据子集(例如块甚至数据)进行操作一次 csv 的一行)。如果您可以重构方法以实现这种方式,则可以利用任务或并行 foreach 循环,以便该测试并行执行。假设你的方法被重构来处理你的 csv 行,你可以这样做:In order to parallelize this unit test, you'll need
doStuffA()
anddoStuffB()
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:这可能看起来有点复杂,但请听我说完。
MSTest 存在一个限制,即您实际上无法并行运行数据驱动测试。
我过去为解决这个问题所做的就是在 Visual Studio 中创建一个“自定义工具”。
https://msdn.microsoft.com/en-us/library/bb166508。 aspx
或
https://msdn.microsoft.com/en- us/library/bb166817.aspx
我们创建的自定义工具执行以下操作:
当生成这些测试时,我们在它们上添加特定的测试属性,因此我们可以指定仅运行具有该属性的测试。
这听起来有点夸张,但如果您很好地构建了自定义工具,那么这实际上是一个非常顺利的过程。
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:
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.
如果 xml 中没有数据被更改(或者方法没有修改 xml 的相同部分),那么您可以这样做。
如果您复制并粘贴某种伪代码,这可能无法运行。
不完全并行,但至少会在同一时间运行!
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..
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!
据我所知:测试中的各个数据行不是并行运行的。但如果您有多个单元测试,它们确实会并行运行。
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.