Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit Categories in combination?

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.

like image 750
EightyOne Unite Avatar asked Jul 29 '09 11:07

EightyOne Unite


People also ask

What is category attribute in NUnit?

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.

Is NUnit deprecated?

NUnit 3.7. The AssertionHelper class has now been deprecated.

What is TestFixture NUnit?

The [TestFixture] attribute denotes a class that contains unit tests. The [Test] attribute indicates a method is a test method. Save this file and execute dotnet test to build the tests and the class library and then run the tests. The NUnit test runner contains the program entry point to run your tests.

What is NUnit TestContext?

Each NUnit test runs in an execution context, which includes information about the environment as well as the test itself. The TestContext class allows tests to access certain information about the execution context.


6 Answers

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.

like image 87
J4S0Nc Avatar answered Jan 05 '23 18:01

J4S0Nc


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.

like image 39
Mike Two Avatar answered Jan 05 '23 19:01

Mike Two


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

like image 23
Evgeniy Kosjakov Avatar answered Jan 05 '23 17:01

Evgeniy Kosjakov


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

nunit3-console.exe youdll.dll --where="cat==yourCat"
like image 32
Voigt Avatar answered Jan 05 '23 17:01

Voigt


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

like image 21
Pedro Avatar answered Jan 05 '23 18:01

Pedro


As far as I know you can't chose both of them as NUnit stands.

I tried a number of different things with NUnit and the way that my tests were created with no success.

I have found a site that talks you through the process of creating custom category attributes but still can't see how that may help.

like image 36
AutomatedTester Avatar answered Jan 05 '23 19:01

AutomatedTester