執行多樣化選擇的單元測試Running 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 test are run. 下列範例使用dotnet testThe following examples use dotnet test. 若要使用vstest.console.exe,請以取代 --testcasefilter: 取代 --filterIf you're using vstest.console.exe, replace --filter with --testcasefilter:.

MSTestMSTest

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MSTestNamespace
{
    [TestClass]
    public class UnitTest1
    {
        [TestCategory("CategoryA")]
        [Priority(1)]
        [TestMethod]
        public void TestMethod1()
        {
        }

        [Priority(2)]
        [TestMethod]
        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 which 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 which are annotated with [TestCategory("CategoryA")].
dotnet test --filter Priority=2 執行標示有 [Priority(2)] 註釋的測試。Runs tests which are annotated with [Priority(2)].

使用條件式運算子 | 與 &Using conditional operators | and &

運算式Expression 結果Result
dotnet test --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA" 執行 FullyQualifiedName TestCategory 中之 UnitTest1CategoryA 的測試。Runs tests which have UnitTest1 in FullyQualifiedName or TestCategory is CategoryA.
dotnet test --filter "FullyQualifiedName~UnitTest1&TestCategory=CategoryA" 執行 FullyQualifiedName TestCategory 中之 UnitTest1CategoryA 的測試。Runs tests which have UnitTest1 in FullyQualifiedName and TestCategory is CategoryA.
dotnet test --filter "(FullyQualifiedName~UnitTest1&TestCategory=CategoryA)|Priority=1" 執行 FullyQualifiedName 所含之 UnitTest1 TestCategoryCategoryA Priority 為 1 的測試。Runs tests which have either FullyQualifiedName containing UnitTest1 and TestCategory is CategoryA or Priority is 1.

xUnitxUnit

using Xunit;

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

        [Trait("Priority", "2")]
        [Fact]
        public void Test2()
        {
        }
    }
}
運算式Expression 結果Result
dotnet test --filter DisplayName=XUnitNamespace.TestClass1.Test1 僅執行 XUnitNamespace.TestClass1.Test1 這一項測試。Runs 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.

在程式碼範例中,所定義具有索引鍵 CategoryPriority 的特徵可用於篩選。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 which have [Trait("Category", "CategoryA")].

使用條件式運算子 | 與 &Using conditional operators | and &

運算式Expression 結果Result
dotnet test --filter "FullyQualifiedName~TestClass1|Category=CategoryA" 執行 FullyQualifiedName Category 中之 TestClass1CategoryA 的測試。Runs tests which has TestClass1 in FullyQualifiedName or Category is CategoryA.
dotnet test --filter "FullyQualifiedName~TestClass1&Category=CategoryA" 執行 FullyQualifiedName Category 中之 TestClass1CategoryA 的測試。Runs tests which has TestClass1 in FullyQualifiedName and Category is CategoryA.
dotnet test --filter "(FullyQualifiedName~TestClass1&Category=CategoryA)|Priority=1" 執行 FullyQualifiedName 所含之 TestClass1 CategoryCategoryA Priority 為 1 的測試。Runs tests which have either FullyQualifiedName containing TestClass1 and Category is CategoryA or Priority is 1.

NUnitNUnit

using NUnit.Framework;

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

        [Property("Priority", 2)]
        [Test]
        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 which 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 which are annotated with [Category("CategoryA")].
dotnet test --filter Priority=2 執行標示有 [Priority(2)] 註釋的測試。Runs tests which are annotated with [Priority(2)].

使用條件式運算子 | 與 &Using conditional operators | and &

運算式Expression 結果Result
dotnet test --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA" 執行 FullyQualifiedName TestCategory 中之 UnitTest1CategoryA 的測試。Runs tests which have UnitTest1 in FullyQualifiedName or TestCategory is CategoryA.
dotnet test --filter "FullyQualifiedName~UnitTest1&TestCategory=CategoryA" 執行 FullyQualifiedName TestCategory 中之 UnitTest1CategoryA 的測試。Runs tests which have UnitTest1 in FullyQualifiedName and TestCategory is CategoryA.
dotnet test --filter "(FullyQualifiedName~UnitTest1&TestCategory=CategoryA)|Priority=1" 執行 FullyQualifiedName 所含之 UnitTest1 TestCategoryCategoryA Priority 為 1 的測試。Runs tests which have either FullyQualifiedName containing UnitTest1 and TestCategory is CategoryA or Priority is 1.