如何配置 Teamcity 忽略一些测试

How to configure Teamcity to ignore some tests

有没有办法配置 Teamcity 忽略一些测试?我只需要在本地 运行 这些测试,当它们 运行 在 Teamcity 中时,必须忽略。

我正在使用 nunit。

这可以是指令、属性等。

您可以通过向测试中添加测试类别来实现。

[Category("LocalOnly")]
[Test]
public void MyLocalTest()
{
    // Code omitted for brevity
}

然后您可以在 TeamCity 构建步骤中将该类别添加到 NUnit runner 的 'NUnit categories exclude:' 字段。

NUnit categories exclude: LocalOnly

TeamCity 9.1 supports NUnit 3 and it opens many other possibilities to select tests for executing or filter them out. I would recommend to use --where=EXPRESSION which allows to use Test Selection Language。现在您甚至可以使用正则表达式来指定要 运行 或排除的测试。

例子

您只想排除一项测试吗?

--where="method != 'TestName'"

您只想排除一项测试吗?记不太清名字了,记得带"BuggyMethod"的东西(~表示涉及正则表达式):

--where="method !~ 'BuggyMethod'"

运行 所有测试定义在一个 class:

--where="class == 'My.Namespace.ClassName'"

忘记了完整的命名空间?这不再是问题 - 使用正则表达式:

--where="class =~ 'ClassName'"

您也可以组合这些表达式来达到预期的效果。 运行 class 的所有测试,但排除包含 "BuggyMethod":

的所有方法
--where="class =~ 'ClassName' and method !~ 'BuggyMethod'"

这种方法更加灵活,可以避免对代码进行任何修改。我看不出再使用类别有什么意义,除非您的测试 class 使用类别进行了验证。