31
votes

We've got some integration tests in our solution. To run these tests, simulation software must be installed on the developer PC. This software is, however, not installed on every developer PC. If the simulation software is not installed, these tests should be skipped, otherwise ==> NullRefException.

I'm now seeking for a way to do a "conditional ignore" for tests/testfixtures. Something like

if(simulationFilesExist) 
do testfixture
else skip testfixture

NUnit gives some useful things like ignore and explicit but that's not quiet what I need.

4

4 Answers

52
votes

Use some code in your test or fixture set up method that detects if the simulation software is installed or not and calls Assert.Ignore() if it isn't.

[SetUp]
public void TestSetUp() 
{
     if (!TestHelper.SimulationFilesExist())
     {
         Assert.Ignore( "Simulation files are not installed.  Omitting." );
     }
}

or

[TestFixtureSetUp]
public void FixtureSetUp()
{
     if (!TestHelper.SimulationFilesExist())
     {
         Assert.Ignore( "Simulation files are not installed.  Omitting fixture." );
     }
}

In NUnit 3.0 and higher you have to use OneTimeSetUp attribute instead of TestFixtureSetUp.

7
votes

NUnit also gives you the option to supply a Category attribute. Depending on how you are launching your tests, it may be appropriate to flag all the tests that require the simulator with a known category (e.g. [Category("RequiresSimulationSoftware")]). Then from the NUnit Gui you can choose to exclude certain categories. You can do the same thing from the NUnit command line runner (specify /exclude:RequiresSimulationSoftware if applicable).

Hope this (or the previous answer by tvanfosson) helps.

2
votes
[SetUp]
public void TestSetUp() 
{
     if (!TestHelper.SimulationFilesExist())
     {
         Assert.Ignore( "Simulation files are not installed.  Omitting." );
     }
}

you use this type of condition in TestFixtureSet Attribute. But if this fixture have parametreized test then if you want to ignore parameterized test of this fixture then this goes in infinite loop and your test will be hanged. So you use setup attribute better for if condition.

0
votes

I didn't want to duplicate Assert.Ignore condition in every test case, so I ended up using a custom Attribute class, which I derived from the NUnitAttribute class:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class SimulatorOnlyAttribute : NUnitAttribute, IApplyToTest
{
    public void ApplyToTest(Test test)
    {
        if (test.RunState == RunState.NotRunnable)
        {
            return;
        }

        if (!Helper.RunsOnSimulator)
        {
            test.RunState = RunState.Ignored;
            test.Properties.Set(PropertyNames.SkipReason, "This test should run only on simulator");
        }
    }
}

So now I can just mark required test cases with the new attribute:

[SimulatorOnly]
public void Test()

For reference you could investigate source code of the IgnoreAttribute