learner
learner

Reputation: 109

How to categorize test classes and test methods and select specific categories for testing in nunit?

Is it possible to categorize test classes or test methods for example lets say as BASIC,PERFORMANCE and SIMPLE in nunit and then allow the user to decide which category of tests he wants to run. Can the nunit extensions help in this in anyway? An explanation with an example will be highly appreciable. PS: i am currently using nunit 2.6.4

Upvotes: 1

Views: 1678

Answers (2)

Kritner
Kritner

Reputation: 13765

Yes you can categorize tests on either the class or the test level, and tests can have multiple categories as well.

One option as mentioned is the [TestCategory("MyCategory")] route, but I prefer to just create new attributes so i can have a more "strongly" typed set of categories to choose from.

Example:

public class FastIntegrationTestAttribute : CategoryAttribute { }

public class LongRunningIntegrationTestAttribute : CategoryAttribute { }

public class NightlyTestAttribute : CategoryAttribute { }

In action:

[TestFixture, FastIntegrationTest]
public class MyClassThatNeedsTesting
{

    [Test]
    [NightlyTest]
    public void MyTest()
    {
    }

    [Test]
    public void AnotherTest()
    {
    }
}

Note that the fixture level attribute applies that attribute to all tests, so MyTest has both a "FastIntegrationTest" and "NightlyTest" attribute, where the AnotherTest just has the "FastIntegrationTest" attribute.

There are various ways to run specific categories of test, depending on your runner, and framework version.

in dotnetcore as an example, you can do:

dotnet test --where "cat == NightlyTest"

The resharper test runner also allows grouping by category, in addition to the methods @Charlie mentioned

Upvotes: 3

Charlie
Charlie

Reputation: 13726

You apply categories to tests or fixtures using the CategoryAttribute.

Then, you can run them in various ways, depending on the runner you use. For example...

  • NUnit 2.6.4 console runner - use the /include and/or /exclude options on the command-line.

  • NUnit 2.6.4 GUI runner - select the categories to run in the gui

  • NUnit VS adapter running on Visual Studio - sort tests by trait and select the category that you want to run.

Upvotes: 1

Related Questions