将参数或选项从 VSTest.Console.exe 传递给单元测试 [英] Pass argument or option to Unit Tests from VSTest.Console.exe

查看:36
本文介绍了将参数或选项从 VSTest.Console.exe 传递给单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从命令行成功运行 VS 单元测试(因此从构建机器上的构建).

I can successfully run the VS unit tests from the command line (and hence from the build on the build machine).

VSTest.Console.EXE "MyTest.dll" /logger:trx /platform:x64 /inIsolation

我还可以使用 /TestCaseFilter 选项过滤掉我不想在特定环境中执行的任何必需测试:

I can also filter out any required tests that I don't want to execute on a certain environment with /TestCaseFilter option:

VSTest.Console.EXE "MyTest.dll" /TestCaseFilter:Name!=Verify_DigitallySigned

这是不需要运行检查是否数字签名"测试所必需的.

This is needed to not to run "check if digitally signed" test(s).

这样我就可以过滤掉所需的测试用例集.

This way I can filter out the required set of test case/s.

但是,我想要的是让单元测试知道是否不需要某些测试(断言).例如传递一个 "/DontTestSigning" 参数.这样,单元测试(用 C++ 或 C# 编写)会看到这样的参数/选项,并且不会做额外的断言,从而防止在非实际生产构建(例如 PR 构建)上的构建失败.

However, what I want is to let the unit test know if certain tests (asserts) are not required. For example passing a "/DontTestSigning" argument. This way the unit tests (written in C++ or C#) would see such parameter/option, and would not do additional asserts, thus preventing the build failures on not-real production builds (such as on PR builds).

我看到 VSTest.Console.exe 有 /testsettings 选项(还有 MSTest.exe),但我不确定(如果)可以应用并让实际的测试功能知道一些不做"的选项.

I see that there is /testsettings option with VSTest.Console.exe (and with MSTest.exe also), but I am not sure how (IF) that can be applied and letting the actual test functions to know about some "dont-do" option.

推荐答案

您还可以向 vstest.console-process 提供 .runsettings-file,如此处所示.https://docs.microsoft.com/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file?view=vs-2019.

You can also provide a .runsettings-file to the vstest.console-process, as indicated here. https://docs.microsoft.com/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file?view=vs-2019.

为了提供自定义参数,请修改 TestRunParameters-section,例如:

In order to provide custom parameters modify the TestRunParameters-section, e.g:

<!-- Parameters used by tests at runtime -->
<TestRunParameters>
  <Parameter name="executeAsserts" value="1,2,3" />
</TestRunParameters>

现在可以通过以下代码访问这些参数:

These parameters can now be accessed via this code:

TestContext.Properties["executeAsserts"];

这样您的最终测试代码可能如下所示:

so that your final test-code may look like this:

[Test]
public void MyTest()
{
    var assertsToRun = TestContext.Properties["executeAsserts"].Split(",").Select(x => Convert.ToInt(x)).ToArray();
    if(assertsToRun.Contains(1)
        Assert.That(...);
    if(assertsToRun.Contains(2)
        Assert.That(...);
    if(assertsToRun.Contains(3)
        Assert.That(...);
}

您应该能够使用以下命令运行测试:

You should be able to run the test using the following command:

vstest.console.exe MyTestAssembly.dll /Settings:MySettings.runsettings

这篇关于将参数或选项从 VSTest.Console.exe 传递给单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆