我期望 vstest.console.exe 将与指定的 /Parallel
选项并行运行所有测试方法。在 4 核机器上,我预计下面的测试类将花费大约 2~3 秒来执行,实际上我得到了 8~9 秒,这意味着测试是按顺序执行的。
[TestClass]
public class UnitTest1
{
[TestMethod]
public async Task TestMethod1()
{
await Task.Delay(2000);
}
[TestMethod]
public async Task TestMethod2()
{
await Task.Delay(2000);
}
[TestMethod]
public async Task TestMethod3()
{
await Task.Delay(2000);
}
[TestMethod]
public async Task TestMethod4()
{
await Task.Delay(2000);
}
}
测试输出:
Microsoft (R) Test Execution Command Line Tool Version 15.5.0 Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait... Passed TestMethod1 Passed
TestMethod2 Passed TestMethod3 Passed TestMethod4Total tests: 4. Passed: 4. Failed: 0. Skipped: 0. Test Run Successful.
Test execution time: 8.6960 Seconds
最佳答案
您将需要一个设置文件来告诉您要使用多少核心来执行。
命令是这样的:
vstest.console.exe /Parallel MyUnitTest.dll /Settings:C:\Settings.testsettings
Settings.testsettings 应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>0</MaxCpuCount>
</RunConfiguration>
</RunSettings>
MaxCpuCount 的值具有以下语义:
‘n’(其中 1 <= n <= 核心数):将启动最多‘n’个进程。 任何其他值的“n”:启动的进程数将与机器上的可用内核一样多。 通常,值为 0 表示最多可以使用所有可用的空闲内核。
关于c# - vstest.console.exe 的并行选项无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48703056/