借助 .NET Core 中的 dotnet test 命令,可以使用筛选表达式来运行选定的测试。 本文演示如何筛选测试。 本示例使用 dotnet test。 如果使用的是 vstest.console.exe,请将 --filter 替换成 --testcasefilter:。
语法
dotnet test --filter <Expression>
表达式的格式为 。
表达式可与布尔运算符结合使用:
|表示 or 布尔运算符, 表示 and 布尔运算符 。表达式可以用括号括起来。 例如:
(Name~MyClass) | (Name~MyClass2)。没有任何运算符的表达式被解释为 属性上的 contains 。 例如,
dotnet test --filter xyz和dotnet test --filter FullyQualifiedName~xyz相同。属性是 的一个特性。 例如,常用单元测试框架支持以下属性。
测试框架 支持的属性 MSTest FullyQualifiedNameNameClassNamePriorityTestCategoryxUnit FullyQualifiedNameDisplayNameTraitsNunit FullyQualifiedNameNamePriorityTestCategory运算符
-
=完全匹配 -
!=非完全匹配 -
~包含 -
!~不包含
-
值是一个字符串。 所有查找都不区分大小写。
字符转义
若要在筛选表达式中使用感叹号 (!),必须在 Linux 或 macOS shell 中对其进行转义,具体做法是在前面加一个反斜杠 (\!)。 例如,以下筛选器跳过包含 IntegrationTests 的命名空间中的所有测试:
dotnet test --filter FullyQualifiedName\!~IntegrationTests
对于包含泛型类型参数的逗号的 FullyQualifiedName 值,请使用 %2C 来转义逗号。 例如:
dotnet test --filter "FullyQualifiedName=MyNamespace.MyTestsClass<ParameterType1%2CParameterType2>.MyTestMethod"
对于 Name 或 DisplayName,请使用特殊字符的 URL 编码。 例如,若要运行名称为 MyTestMethod 的测试,以及 "text"字符串值,请使用以下筛选器:
dotnet test --filter "Name=MyTestMethod \(%22text%22\)"
MSTest 示例
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MSTestNamespace
{
[TestClass]
public class UnitTest1
{
[TestMethod, Priority(1), TestCategory("CategoryA")]
public void TestMethod1()
{
}
[TestMethod, Priority(2)]
public void TestMethod2()
{
}
}
}
| 表达式 | 结果 |
|---|---|
dotnet test --filter Method |
运行 FullyQualifiedName 包含 Method 的测试。 |
dotnet test --filter Name~TestMethod1 |
运行名称包含 TestMethod1 的测试。 |
dotnet test --filter ClassName=MSTestNamespace.UnitTest1 |
运行属于类 MSTestNamespace.UnitTest1 的测试。注意: 由于 ClassName 值应有命名空间,因此 ClassName=UnitTest1 无效。 |
dotnet test --filter FullyQualifiedName!=MSTestNamespace.UnitTest1.TestMethod1 |
运行除 MSTestNamespace.UnitTest1.TestMethod1 之外的其他所有测试。 |
dotnet test --filter TestCategory=CategoryA |
运行含 [TestCategory("CategoryA")] 批注的测试。 |
dotnet test --filter Priority=2 |
运行含 [Priority(2)] 批注的测试。 |
使用条件运算符 | 和 & 的示例:
运行
UnitTest1中包含 FullyQualifiedName 或TestCategoryAttribute 为"CategoryA"的测试。dotnet test --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"运行
UnitTest1中包含 FullyQualifiedName 且TestCategoryAttribute 为"CategoryA"的测试。dotnet test --filter "FullyQualifiedName~UnitTest1&TestCategory=CategoryA"运行 FullyQualifiedName 中包含
UnitTest1且TestCategoryAttribute 为"CategoryA"或PriorityAttribute 的优先级为1的测试。dotnet test --filter "(FullyQualifiedName~UnitTest1&TestCategory=CategoryA)|Priority=1"
xUnit 示例
using Xunit;
namespace XUnitNamespace
{
public class TestClass1
{
[Fact, Trait("Priority", "1"), Trait("Category", "CategoryA")]
public void Test1()
{
}
[Fact, Trait("Priority", "2")]
public void Test2()
{
}
}
}
| 表达式 | 结果 |
|---|---|
dotnet test --filter DisplayName=XUnitNamespace.TestClass1.Test1 |
仅运行一个测试,即 XUnitNamespace.TestClass1.Test1。 |
dotnet test --filter FullyQualifiedName!=XUnitNamespace.TestClass1.Test1 |
运行除 XUnitNamespace.TestClass1.Test1 之外的其他所有测试。 |
dotnet test --filter DisplayName~TestClass1 |
运行显示名称包含 TestClass1 的测试。 |
在代码示例中,包含键 "Category" 和 "Priority" 的已定义特征可用于筛选。
| 表达式 | 结果 |
|---|---|
dotnet test --filter XUnit |
运行 FullyQualifiedName 包含 XUnit 的测试。 |
dotnet test --filter Category=CategoryA |
运行包含 [Trait("Category", "CategoryA")] 的测试。 |
使用条件运算符 | 和 & 的示例:
运行
TestClass1中包含 FullyQualifiedName 或Trait的键为"Category"且值为"CategoryA"的测试。dotnet test --filter "FullyQualifiedName~TestClass1|Category=CategoryA"运行
TestClass1中包含 FullyQualifiedName 且Trait的键为"Category"且值为"CategoryA"的测试。dotnet test --filter "FullyQualifiedName~TestClass1&Category=CategoryA"运行 FullyQualifiedName 中包含
TestClass1且Trait的键为"Category"且值为"CategoryA"或Trait的键为"Priority"且值为1的测试。dotnet test --filter "(FullyQualifiedName~TestClass1&Category=CategoryA)|Priority=1"
NUnit 示例
using NUnit.Framework;
namespace NUnitNamespace
{
public class UnitTest1
{
[Test, Property("Priority", 1), Category("CategoryA")]
public void TestMethod1()
{
}
[Test, Property("Priority", 2)]
public void TestMethod2()
{
}
}
}
| 表达式 | 结果 |
|---|---|
dotnet test --filter Method |
运行 FullyQualifiedName 包含 Method 的测试。 |
dotnet test --filter Name~TestMethod1 |
运行名称包含 TestMethod1 的测试。 |
dotnet test --filter FullyQualifiedName~NUnitNamespace.UnitTest1 |
运行属于类 NUnitNamespace.UnitTest1 的测试。 |
dotnet test --filter FullyQualifiedName!=NUnitNamespace.UnitTest1.TestMethod1 |
运行除 NUnitNamespace.UnitTest1.TestMethod1 之外的其他所有测试。 |
dotnet test --filter TestCategory=CategoryA |
运行含 [Category("CategoryA")] 批注的测试。 |
dotnet test --filter Priority=2 |
运行含 [Priority(2)] 批注的测试。 |
使用条件运算符 | 和 & 的示例:
运行 UnitTest1 中包含 FullyQualifiedName 或Category 为 "CategoryA" 的测试。
dotnet test --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"
运行 UnitTest1 中包含 FullyQualifiedName 且Category 为 "CategoryA" 的测试。
dotnet test --filter "FullyQualifiedName~UnitTest1&TestCategory=CategoryA"
运行 FullyQualifiedName 中包含 UnitTest1 且Category 为 "CategoryA" 或Property 的 "Priority" 为 1 的测试。
dotnet test --filter "(FullyQualifiedName~UnitTest1&TestCategory=CategoryA)|Priority=1"
有关详细信息,请参阅 TestCase 筛选器