Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore Test or TestFixture based on condition

Tags:

nunit

People also ask

How do I ignore test class in NUnit?

By not applying the attribute you're ignoring the fact that there are test methods within this class that aren't being tested for a reason. You should use [TestFixture, Ignore("reason")] to supply the output of the test results with a reason why they're ignored.

What is the use of TestFixture attribute?

The [TestFixture] attribute at the beginning indicates that this class is a test fixture so that NUnit can identify it as a runnable test class. Similarly NUnit uses attributes to indicate various properties of classes/methods. Then you see two methods tagged [SetUp] and [TearDown].

How do I cancel my NUnit test?

Using nunit-console, you can achieve this by using the /stoponerror command line parameter. See here for the command line reference. For nunit-console v3, it changes to --stoponerror (see here for the command line reference).

How do I use ignore attribute?

The ignore attribute is an attribute to not run a test or test fixture for a period of time. The person marks either a Test or a TestFixture with the Ignore Attribute. The running program sees the attribute and does not run the test or tests.


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.


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.


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