c# - vstest.console.exe 的并行选项无法按预期工作

标签 c# visual-studio unit-testing async-await mstest

我期望 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 TestMethod4

Total 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/

相关文章:

c# - 异步回发不起作用

c# - 将 UI 对象链接在一起 : could I do it with attributes?

c# - 无法使用 Xunit 在异步方法中断言异常

c# - 编译需要 bindingRedirect 的 C# 库项目

c++ - 由于生成后步骤,未加载 native dll 的符号 (pdb)

Angular6 ngrx : Cannot read property 'ids' of undefined

php - Laravel Dusk,如何在测试之间销毁 session 数据

c# - WebBrowser.Navigate 重载不添加 cookie

java - SpringBoot @WebMvcTest 和 @MockBean 没有按预期工作

windows - 在 cmake/Visual Studio 项目中调试/运行可执行文件