运行选定的单元测试

借助 .NET Core 中的 dotnet test 命令,可以使用筛选表达式来运行选定的测试。 本文演示如何筛选测试。 本示例使用 dotnet test。 如果使用的是 vstest.console.exe,请将 --filter 替换成 --testcasefilter:

语法

dotnet test --filter <Expression>
  • 表达式的格式为 <Property><Operator><Value>[|&<Expression>]

    表达式可与布尔运算符结合使用:| 表示 or 布尔运算符,& 表示 and 布尔运算符 。

    表达式可以用括号括起来。 例如:(Name~MyClass) | (Name~MyClass2)

    没有任何运算符的表达式被解释为 FullyQualifiedName 属性上的 contains 。 例如,dotnet test --filter xyzdotnet test --filter FullyQualifiedName~xyz 相同。

  • 属性是 Test Case 的一个特性。 例如,常用单元测试框架支持以下属性。

    测试框架 支持的属性
    MSTest FullyQualifiedName
    Name
    ClassName
    Priority
    TestCategory
    xUnit FullyQualifiedName
    DisplayName
    Traits
    Nunit FullyQualifiedName
    Name
    Priority
    TestCategory
  • 运算符

    • = 完全匹配
    • != 非完全匹配
    • ~ 包含
    • !~ 不包含
  • 值是一个字符串。 所有查找都不区分大小写。

字符转义

若要在筛选表达式中使用感叹号 (!),必须在 Linux 或 macOS shell 中对其进行转义,具体做法是在前面加一个反斜杠 (\!)。 例如,以下筛选器跳过包含 IntegrationTests 的命名空间中的所有测试:

dotnet test --filter FullyQualifiedName\!~IntegrationTests

对于包含泛型类型参数的逗号的 FullyQualifiedName 值,请使用 %2C 来转义逗号。 例如:

dotnet test --filter "FullyQualifiedName=MyNamespace.MyTestsClass<ParameterType1%2CParameterType2>.MyTestMethod"

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)] 批注的测试。

使用条件运算符 |& 的示例:

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")] 的测试。

使用条件运算符 |& 的示例:

  • 运行 FullyQualifiedName 中包含 TestClass1Trait 的键为 "Category" 且值为 "CategoryA" 的测试。

    dotnet test --filter "FullyQualifiedName~TestClass1|Category=CategoryA"
    
  • 运行 FullyQualifiedName 中包含 TestClass1,并且 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)] 批注的测试。

使用条件运算符 |& 的示例:

运行 FullyQualifiedName 中包含 UnitTest1Category"CategoryA" 的测试。

dotnet test --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"

运行 FullyQualifiedName 中包含 UnitTest1Category"CategoryA" 的测试。

dotnet test --filter "FullyQualifiedName~UnitTest1&TestCategory=CategoryA"

运行 FullyQualifiedName 中包含 UnitTest1Category"CategoryA"Property"Priority"1 的测试。

dotnet test --filter "(FullyQualifiedName~UnitTest1&TestCategory=CategoryA)|Priority=1"

有关详细信息,请参阅 TestCase 筛选器

请参阅

后续步骤