1

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.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Did you try `cat !~ '.+'`? – Klaus Gütter May 10 '21 at 10:01
  • @KlausGütter Wow, a regex for a simple null-check. It gets its job done, though. However I can´t imagine this to be the only way. – MakePeaceGreatAgain May 10 '21 at 10:33
  • 1
    Indeed. But look at the `Match` method in the [source](https://github.com/nunit/nunit/blob/master/src/NUnitFramework/framework/Internal/Filters/CategoryFilter.cs): a missing category will de evaluated as false, so a negation is needed. – Klaus Gütter May 10 '21 at 11:15

1 Answers1

0

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

So the condition must include a negation. As you already observed, "cat != 'MyCategory'" will include the tests without category, but will 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 !~ '.+'"
Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36