未设置类别条件的 nunit

nunit where condition for categoriy not being set

我的测试中有一堆测试用例-class:

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

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

我现在想要实现的是运行只有没有使用nunit-console设置类别的测试。所以在我的例子中只有 Test1 应该 运行.

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

我也试过 "cat == ''" 结果相同。

但是那不会给我 任何 测试。当然,我可以在这个人为的示例中使用 "cat != 'MyCategory'",但是由于我有很多类别,所以我不想排除所有类别,而是只选择那些 而没有 任何类别。

查看 source 中的 Match 方法表明:缺少的类别将被取消评估为 false。

所以条件必须包含否定。正如您已经观察到的,"cat != 'MyCategory'" 将包括没有类别的测试,但将包括类别不是 'MyCategory' 的测试。

要排除具有 any 类别的所有测试,您可以使用 !~ 运算符(正则表达式不匹配),例如

where "cat !~ '.+'"