NUnit Categories in combination?

The quick answer from 2019 for NUnit 3.0. It's possible to use multiple categories.

Consider the following example:


    [TestFixture]
    public class Tests1
    {
        [Test]
        [Category("A")]
        public void TestA()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("B")]
        public void TestB()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("C")]
        public void TestC()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("A")]
        [Category("B")]
        public void TestAB()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("A")]
        [Category("C")]
        public void TestAC()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("B")]
        [Category("C")]
        public void TestBC()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("A")]
        [Category("B")]
        [Category("C")]
        public void TestABC()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }
    }

You can run the tests with both categories A and B and excluding category C using this command argument: --where="cat==A && cat==B && cat!=C"

enter image description here

Or you can run the tests with any of categories A and B and excluding C like this: --where="(cat==A || cat==B) && cat!=C"

enter image description here


If you use version 3.0 use the option --where. Example:

nunit3-console.exe youdll.dll --where="cat==yourCat"

No. There is no way to only run tests that belong to two, or more, specific categories. To be honest when we first put the feature in several years ago I never thought of that. We tried to keep it as simple as possible.

By the way, you don't need to specify [Test] twice on your test3 method.

[Test]
[Category("catA")]
[Category("catB")]
public void test3
{
    //
}

Not that it makes a difference. It's just a style preference.


based on the docs, you just say /include:CatA+CatB

http://www.nunit.org/index.php?p=consoleCommandLine&r=2.5.1

Specifying Test Categories to Include or Exclude

NUnit provides CategoryAttribute for use in marking tests as belonging to one or more categories. Categories may be included or excluded in a test run using the /include and /exclude options. The following command runs only the tests in the BaseLine category:

nunit-console myassembly.dll /include:BaseLine The following command runs all tests except those in the Database category:

nunit-console myassembly.dll /exclude:Database Multiple categories may be specified on either option, by using commas to separate them.

Notes: Beginning with NUnit 2.4, the /include and /exclude options may be combined on the command line. When both are used, all tests with the included categories are run except for those with the excluded categories.

Beginning with NUnit 2.4.6, you may use a Category Expression with either of these options:

  • A|B|C Selects tests having any of the categories A, B or C.
  • A,B,C Selects tests having any of the categories A, B or C.
  • A+B+C Selects only tests having all three of the categories assigned
  • A+B|C Selects tests with both A and B OR with category C.
  • A+B-C Selects tests with both A and B but not C.
  • -A Selects tests not having category A assigned
  • A+(B|C) Selects tests having both category A and either of B or C The comma operator is equivalent to | but has a higher precendence. Order of evaluation is as follows:

    Unary exclusion operator (-) High-precendence union operator (,) Intersection and set subtraction operators (+ and binary -) Low-precedence union operator (|) Note: Because the operator characters have special meaning, you should avoid creating a category that uses any of them in it's name. For example, the category "db-tests" could not be used on the command line, since it appears to means "run category db, except for category tests." The same limitation applies to characters that have special meaning for the shell you are using.