Specifying multiple categories in nUnit

posted by Bryan on

The CategoryAttribute can be used to specify additional groupings of test cases that you want to run at the same time. For example, we have our test cases split up into level of priorities (Level 1, Level 2, Level 3). Here's how you can do it:

First, you can specify the Category on the TestFixture or on the Test itself. Examples:

[TestFixture]
[Category("LongRunning")]
[Description("These are the long running tests of the application")]
public void CreatePaymentsTests
{

        [Test, Category("Smoke"), Description("A one-time payment")]
        public void verifyCreatePayment()
        {
               //test contents
        }

}

In this case, when running NUnit from the Gui - you could select the Categories tab and both categories will be shown to you. If you select the LongRunning category, it will run all the tests in the TestFixture. If you select the Smoke category, it will only run the 1 test.

However, what if you have a Level 3 test case (not a high priority feature) but you want to run with the Smoke tests category, just to make sure the build is working correctly. This is how you do it.

        [Test, Category("Smoke"), Category("L3"), Description("A one-time payment")]
        public void verifyCreatePayment()
        {
               //test contents
        }

Now this test will be picked up by either choosing LongRunning, Smoke, or L3. If more than one of those categories are chosen, the test will still only run 1 time. Just adds a bit of flexibility when running the suite.

It would be nice to have the parameter on the CategoryAttribute be a var args or something, but as of version 2.5.5, that doesn't be the case.

Leave a comment

blog comments powered by Disqus