Search code examples
c#nunit

C# NUnit Specify which test classes to run


I have a use case where I need to specify a list of test classes to run.

Each test class has x number of tests. I need to make custom test suites which run only the specified test classes.

Is there anyway to do this? Will they still show in the Test Runner?


Solution

  • Yes you can use Categories docs:

    The Category attribute provides an alternative to suites for dealing with groups of tests. Either individual test cases or fixtures may be identified as belonging to a particular category. Some runners, including the Console Runner, allow specifying categories to be included in or excluded from the run. When categories are used, only the tests in the selected categories will be run. Those tests in categories that are not selected are not reported at all.

    Example:

      [TestFixture]
      [Category("CategoryA")]
      public class CategoryATests
      {
        // ...
      }
    

    And the dotnet test cli has a --filter flag:

    dotnet test --filter <Expression>
    

    The give an example only for mstest Example:

    dotnet test --filter TestCategory=CategoryA 
    

    but it also tell that it supports nunit filtering with FullyQualifiedName, Name, Priority, TestCategory try that out.