執行所選的單元測試

您可以透過 .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。 例如,dotnet test --filter xyzdotnet test --filter FullyQualifiedName~xyz 相同。

  • 屬性 (property) 是 Test Case 的屬性 (attribute)。 例如,下列是常用單元測試架構所支援的屬性 (property)。

    測試架構 支援的屬性
    MSTest FullyQualifiedName
    Name
    ClassName
    Priority
    TestCategory
    Id
    xUnit FullyQualifiedName
    DisplayName
    Traits
    NUnit FullyQualifiedName
    Name
    Priority
    TestCategory
    Category
    Property
  • 運算子

    • = 完全相符
    • != 不完全相符
    • ~ 包含
    • !~ 不包含
  • 是一個字串。 所有的查閱皆不區分大小寫。

字元逸出

使用跳脫序列來表示濾波器值中的濾波器運算子字元。 這些逃脫關卡與你逃脫殼體所需的任何程序是分開的。

逸出順序 代表
\\ \
\( (
\) )
\& &
\| |
\= =
\! !
\~ ~

若要程式化逃逸過濾器值,請使用 Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities.FilterHelper.Escape NuGet 套件中的 Microsoft.VisualStudio.TestPlatform.ObjectModel API。

若要在篩選條件運算式中使用驚嘆號 (!),您必須將反斜線 (\!) 放在驚嘆號前面,以在部分 Linux 或 macOS 殼層中將驚嘆號逸出。 例如,當命名空間包含 IntegrationTests 時,下列篩選會略過其中的所有測試:

dotnet test --filter FullyQualifiedName\!~IntegrationTests

如果FullyQualifiedName 值包含泛型型別參數的逗號,請使用 %2C 將逗號逸出。 例如:

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

因為你的 shell 也會解析篩選式,所以只要包含 shell 特別處理的字元,例如 <>、 ,就,引用整個--filter值。 PowerShell 中必須引用,逗號代表陣列運算子。

針對 NameDisplayName,請使用特殊字元的 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)] 標註的測試。
dotnet test --filter "Id=<guid>" 執行測試案例,發現 TestCase.Id 值與 GUID 完全相符。

Id 過濾需要 MSTest.TestAdapter 3.6 或更新版本。 從 VSTest 發現回傳的 TestCase.Id測試案例中取得 GUID。 此過濾器可選擇一個展開的資料驅動迭代,且不會匹配或逃逸其顯示名稱。 每一列都必須被發現為獨立的測試案例;摺疊式資料驅動測試只會揭露一個父身份。

使用條件運算子 |& 的範例:

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"

    dotnet test --filter "FullyQualifiedName~TestClass1|Category=CategoryA"
    
  • 若要執行 中TestClass1具有 且 具有 索引鍵FullyQualifiedName且 值為 的測試。Trait"Category"

    dotnet test --filter "FullyQualifiedName~TestClass1&Category=CategoryA"
    
  • 若要執行包含 FullyQualifiedNameTestClass1值為 Trait"Category"具有 "CategoryA" 的索引鍵 Trait 和值。"Priority"

    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具有 FullyQualifiedNameCategory"CategoryA"測試。

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

若要執行其 中UnitTest1具有 FullyQualifiedName 具有 Category"CategoryA"測試。

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

若要執行包含 FullyQualifiedNameUnitTest1具有的Category"CategoryA"

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

如需詳細資訊,請參閱 TestCase 篩選

另請參閱

下一步