Pass argument or option to Unit Tests from VSTest.Console.exe

て烟熏妆下的殇ゞ 提交于 2021-02-07 08:27:49

问题


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

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.

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

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.


回答1:


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.

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



回答2:


Basically you should favour to have only a single Assert within your test, so that every tests checks for one single thing.

So what you have is similar to this, I suppose:

[Test]
public void MyTest()
{
    Assert.That(...);
    Assert.That(...);
    Assert.That(...);
}

When you want to exclude e.g. the second Assert, you have of course to provide some functionality in your code to execute or not to execute those lines, e.g.:

public void MyTest()
{
    Assert.That(...);
    if(executeSecondAssert)
        Assert.That(...);
    Assert.That(...);
}

You can introduce some compile-switch that sets the value for the above bool-flag:

#if(EXECUTE_ASSERT)
    bool executeSecondAssert = true;
#else
    bool executeSecondAssert = false;

and now provide that compile-switch via an environment-variable.



来源:https://stackoverflow.com/questions/56806702/pass-argument-or-option-to-unit-tests-from-vstest-console-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!