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.
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.
NUnit 3.7. The AssertionHelper class has now been deprecated.
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.
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.
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 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.
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.
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"
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"
If you use version 3.0 use the option --where
.
Example:
nunit3-console.exe youdll.dll --where="cat==yourCat"
Sounds like what you need is a third category of "catAandB".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With