Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 943
Ajay Avatar asked Dec 31 '22 20:12

Ajay


1 Answers

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.

like image 73
MakePeaceGreatAgain Avatar answered Jan 13 '23 22:01

MakePeaceGreatAgain