NUnit Categories in combination?

20,233

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

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

Solution 5

Sounds like what you need is a third category of "catAandB".

Share:
20,233

Related videos on Youtube

EightyOne Unite
Author by

EightyOne Unite

Updated on July 09, 2022

Comments

  • EightyOne Unite
    EightyOne Unite almost 2 years

    In my NUnit testfixtrues i have something along the lines of

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

    Now in the NUnit gui i want to be able to select catA and catB and run the tests where catA and catB are present. Currently this is not the case and NUnit will run all 3 tests.

    Is there any way to change this behavior to an AND condition rather than OR?

    I'm currently running v2.5.0.9122.

    Thanks in advance.

  • EightyOne Unite
    EightyOne Unite almost 15 years
    yeah, i came across that one too. I'll take another look and have a think how i could go about bending it to my will but i think a change to the nunit gui may be in order for this to work.
  • Anthony
    Anthony over 12 years
    Yes. To give a more concrete example, I have a category called "Integration". I have a second category called "Database" which is a strict subset of "Integration". So "Integration" is like "catAandB".
  • Grimace of Despair
    Grimace of Despair almost 10 years
    This seems to be underreferenced somehow. Never saw that category logic from 2.4.6 before. Thanks for pointing that out.
  • Mike Two
    Mike Two over 8 years
    J4SONc - That must have been added after my time on the project. That's cool. Thanks for pointing that out.
  • bornfromanegg
    bornfromanegg over 4 years
    Viewers of this page should note that this answer is now out of date. It is now possible - see this answer. I only mention it as I nearly missed it.