Search code examples
c#nunitnunit-3.0nunit-console

nunit where condition for categoriy not being set


I have a bunch of testcases within my test-class:

[TestFixture]
class MyTestClass
{
    [Test]
    public void Test1() { ... }

    [Test(Category = "MyCategory")]
    public void Test2() { ... }
}

What I want to achieve now is to run only the tests that have no category set using nunit-console. So in my example only Test1 should run.

nunit3-console.exe MyAssembly.dll where "cat == null"

I also tried "cat == ''" with the same result.

However that won't give me any test. Of course I could use "cat != 'MyCategory'" in this contrived example, however as I have many categories I don't want to exlcude every one but instead chose just those without any category.


Solution

  • A look at the Match method in the source shows: a missing category will be evaluated as false.

    So the condition must include a negation. As you already observed, "cat != 'MyCategory'" will include the tests without category, but will also include tests with a category other than 'MyCategory'.

    To exclude all tests with any category, you can use the !~ operator (regex non-match), e.g.

    where "cat !~ '.+'"