Search code examples
c#asp.net.netunit-testingmstest

How to implement custom [Ignore] attribute for unit tests? (Skip some tests in RELEASE configuration)


I'm using MSTEST. I wish to tag test methods with the [TestWithCheats] attribute. These tests should be skipped without failing if they are run in the RELEASE configuration. However, they must be executed when in DEBUG configuration.

I tried using Assert.Inconclusive in test method, but it always throw an exception.


public static class TestHelpers
{
    public static void SkipIfCheatsNotAllowed()
    {
        #if !ALLOW_CHEATS
        Assert.Inconclusive("Skipping cheat tests in Release mode.");
        #endif
    }
}

TestHelpers.SkipIfCheatsNotAllowed();

So the main problem is these tests failed in jenkins.

Failed TriggerFreeSpinsRejectCashout [2 ms]

Error Message: Assert.Inconclusive failed. 

Skipping cheat test in Release configuration.

Solution

  • Here's the approach I usually take in these situations:

    1. Decorate the test with a category
    [TestCategory("SkipInOfficialBuild")]
    public static void SkipIfCheatsNotAllowed() 
    {
    ...
    }
    
    1. Configure your build pipeline not to run tests with this category. I don't have experience with Maven but if it uses dotnet test it should look something like this:
    dotnet test --filter TestCategory!=SkipInOfficialBuild