运行选择性单元测试Run selective unit tests

借助 .NET Core 中的 dotnet test 命令,可以使用筛选表达式来运行选择性测试。With the dotnet test command in .NET Core, you can use a filter expression to run selective tests. 本文演示如何筛选运行哪些测试。This article demonstrates how to filter which tests are run. 下面的示例使用 dotnet testThe following examples use dotnet test. 如果使用的是 vstest.console.exe,请将 --filter 替换成 --testcasefilter:If you're using vstest.console.exe, replace --filter with --testcasefilter:.

字符转义Character escaping

*nix 上使用包含感叹号 ! 的筛选器需要转义,因为保留了 !Using filters that include exclamation mark ! on *nix requires escaping since ! is reserved. 例如,如果命名空间包含 IntegrationTests,则此筛选器将跳过所有测试:For example, this filter skips all tests if the namespace contains IntegrationTests:

dotnet test --filter FullyQualifiedName\!~IntegrationTests

重要

反斜杠位于感叹号之前,表示它是转义字符 \!The backslash precedes the exclamation mark to indicate it is an escaped character \!.

对于包含泛型类型参数的逗号的 FullyQualifiedName 值,请使用 %2C 来转义逗号。For FullyQualifiedName values that include a comma for generic type parameters, escape the comma with %2C. 例如:For example:

dotnet test --filter "FullyQualifiedName=MyNamespace.MyTestsClass<ParameterType1%2CParameterType2>.MyTestMethod"
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()
        {
        }
    }
}
表达式Expression 结果Result
dotnet test --filter Method 运行 FullyQualifiedName 包含 Method 的测试。Runs tests whose FullyQualifiedName contains Method. vstest 15.1+ 中可用。Available in vstest 15.1+.
dotnet test --filter Name~TestMethod1 运行名称包含 TestMethod1 的测试。Runs tests whose name contains TestMethod1.
dotnet test --filter ClassName=MSTestNamespace.UnitTest1 运行属于类 MSTestNamespace.UnitTest1 的测试。Runs tests that are in class MSTestNamespace.UnitTest1.
注意: 由于 ClassName 值应有命名空间,因此 ClassName=UnitTest1 无效。Note: The ClassName value should have a namespace, so ClassName=UnitTest1 won't work.
dotnet test --filter FullyQualifiedName!=MSTestNamespace.UnitTest1.TestMethod1 运行除 MSTestNamespace.UnitTest1.TestMethod1 之外的其他所有测试。Runs all tests except MSTestNamespace.UnitTest1.TestMethod1.
dotnet test --filter TestCategory=CategoryA 运行含 [TestCategory("CategoryA")] 批注的测试。Runs tests that are annotated with [TestCategory("CategoryA")].
dotnet test --filter Priority=2 运行含 [Priority(2)] 批注的测试。Runs tests that are annotated with [Priority(2)].

使用条件运算符 |& 的示例:Examples using the conditional operators | and &:

运行 FullyQualifiedName 中包含 UnitTest1TestCategoryAttribute"CategoryA" 的测试。To run tests that have UnitTest1 in their FullyQualifiedName or TestCategoryAttribute is "CategoryA".

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

运行 FullyQualifiedName 中包含 UnitTest1TestCategoryAttribute"CategoryA" 的测试。To run tests that have UnitTest1 in their FullyQualifiedName and have a TestCategoryAttribute of "CategoryA".

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

运行 FullyQualifiedName 中包含 UnitTest1TestCategoryAttribute"CategoryA"PriorityAttribute 的优先级为 1 的测试。To run tests that have either FullyQualifiedName containing UnitTest1 and have a TestCategoryAttribute of "CategoryA" or have a PriorityAttribute with a priority of 1.

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

namespace XUnitNamespace
{
    public class TestClass1
    {
        [Fact, Trait("Priority", "1"), Trait("Category", "CategoryA")]
        public void Test1()
        {
        }

        [Fact, Trait("Priority", "2")]
        public void Test2()
        {
        }
    }
}
表达式Expression 结果Result
dotnet test --filter DisplayName=XUnitNamespace.TestClass1.Test1 仅运行一个测试,即 XUnitNamespace.TestClass1.Test1Runs only one test, XUnitNamespace.TestClass1.Test1.
dotnet test --filter FullyQualifiedName!=XUnitNamespace.TestClass1.Test1 运行除 XUnitNamespace.TestClass1.Test1 之外的其他所有测试。Runs all tests except XUnitNamespace.TestClass1.Test1.
dotnet test --filter DisplayName~TestClass1 运行显示名称包含 TestClass1 的测试。Runs tests whose display name contains TestClass1.

在代码示例中,包含键 "Category""Priority" 的已定义特征可用于筛选。In the code example, the defined traits with keys "Category" and "Priority" can be used for filtering.

表达式Expression 结果Result
dotnet test --filter XUnit 运行 FullyQualifiedName 包含 XUnit 的测试。Runs tests whose FullyQualifiedName contains XUnit. vstest 15.1+ 中可用。Available in vstest 15.1+.
dotnet test --filter Category=CategoryA 运行包含 [Trait("Category", "CategoryA")] 的测试。Runs tests that have [Trait("Category", "CategoryA")].

使用条件运算符 |& 的示例:Examples using the conditional operators | and &:

运行 FullyQualifiedName 中包含 TestClass1Trait 的键为 "Category" 且值为 "CategoryA" 的测试。To run tests that have TestClass1 in their FullyQualifiedName or have a Trait with a key of "Category" and value of "CategoryA".

dotnet test --filter "FullyQualifiedName~TestClass1|Category=CategoryA"

运行 FullyQualifiedName 中包含 TestClass1Trait 的键为 "Category" 且值为 "CategoryA" 的测试。To run tests that have TestClass1 in their FullyQualifiedName and have a Trait with a key of "Category" and value of "CategoryA".

dotnet test --filter "FullyQualifiedName~TestClass1&Category=CategoryA"

运行 FullyQualifiedName 中包含 TestClass1Trait 的键为 "Category" 且值为 "CategoryA"Trait 的键为 "Priority" 且值为 1 的测试。To run tests that have either FullyQualifiedName containing TestClass1 and have a Trait with a key of "Category" and value of "CategoryA" or have a Trait with a key of "Priority" and value of 1.

dotnet test --filter "(FullyQualifiedName~TestClass1&Category=CategoryA)|Priority=1"
using NUnit.Framework;

namespace NUnitNamespace
{
    public class UnitTest1
    {
        [Test, Property("Priority", 1), Category("CategoryA")]
        public void TestMethod1()
        {
        }

        [Test, Property("Priority", 2)]
        public void TestMethod2()
        {
        }
    }
}
表达式Expression 结果Result
dotnet test --filter Method 运行 FullyQualifiedName 包含 Method 的测试。Runs tests whose FullyQualifiedName contains Method. vstest 15.1+ 中可用。Available in vstest 15.1+.
dotnet test --filter Name~TestMethod1 运行名称包含 TestMethod1 的测试。Runs tests whose name contains TestMethod1.
dotnet test --filter FullyQualifiedName~NUnitNamespace.UnitTest1 运行属于类 NUnitNamespace.UnitTest1 的测试。Runs tests that are in class NUnitNamespace.UnitTest1.
dotnet test --filter FullyQualifiedName!=NUnitNamespace.UnitTest1.TestMethod1 运行除 NUnitNamespace.UnitTest1.TestMethod1 之外的其他所有测试。Runs all tests except NUnitNamespace.UnitTest1.TestMethod1.
dotnet test --filter TestCategory=CategoryA 运行含 [Category("CategoryA")] 批注的测试。Runs tests that are annotated with [Category("CategoryA")].
dotnet test --filter Priority=2 运行含 [Priority(2)] 批注的测试。Runs tests that are annotated with [Priority(2)].

使用条件运算符 |& 的示例:Examples using the conditional operators | and &:

运行 FullyQualifiedName 中包含 UnitTest1Category"CategoryA" 的测试。To run tests that have UnitTest1 in their FullyQualifiedName or have a Category of "CategoryA".

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

运行 FullyQualifiedName 中包含 UnitTest1Category"CategoryA" 的测试。To run tests that have UnitTest1 in their FullyQualifiedName and have a Category of "CategoryA".

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

运行 FullyQualifiedName 中包含 UnitTest1Category"CategoryA"Property"Priority"1 的测试。To run tests that have either a FullyQualifiedName containing UnitTest1 and have a Category of "CategoryA" or have a Property with a "Priority" of 1.

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

有关详细信息,请参阅 TestCase 筛选器For more information, see TestCase filter.

请参阅See also

后续步骤Next steps