執行所選的單元測試

您可以透過 .NET Core 中的 dotnet test 命令,使用篩選條件運算式來執行所選測試。 本文會示範篩選測試的方式。 範例會使用 dotnet test。 若要使用vstest.console.exe,請以取代 --filter 取代 --testcasefilter:。

Note

--filter以下章節所述的語法適用於 MSTest、NUnit 和 xUnit。 TUnit 使用不同的語法;請選擇本文頂端的 TUnit 以獲取詳細資訊。

語法

dotnet test --filter <Expression>
  • 運算式 的格式為 <Property><Operator><Value>[|&<Expression>]。

    運算式可與布林值運算子相聯結:| 與布林值 or;& 與布林值 and。

    運算式可包含在括號中。 例如: (Name~MyClass) | (Name~MyClass2) 。

    不含任何運算子的運算式會解讀為 屬性上的 FullyQualifiedName。 例如,dotnet test --filter xyz 與 dotnet test --filter FullyQualifiedName~xyz 相同。

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

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

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

字元逸出

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

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

若要程式化逃逸過濾器值,請使用 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 中必須引用,逗號代表陣列運算子。

針對 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)] 標註的測試。
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 範例

對於 NUnit, 和 TestCategoryCategory 是可互換的濾波器屬性名稱。 兩者都會過濾你用屬性指派 Category 的類別,所以現有的 NUnit --filter Category=MyCategory 指令不需要更改。

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 Category=CategoryA 執行與 TestCategory=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"

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

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

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

TUnit 範例

TUnit 不支援 --filter 本文前面提到的選項。 取而代之的是使用MTP --treenode-filter 選項。 樹節點濾波器是形式 /<Assembly>/<Namespace>/<Class>/<Method>為 的路徑,其中:

  • * 匹配單一路徑段內的任何值,且可與其他文字結合,例如 *Method*。
  • ** 匹配任意數量的路徑段。
  • [<Property>=<Value>] 路徑段之後,匹配具有該性質的測試。 [Category("CategoryA")]以性質 , [Property("Priority", "2")] 以及 作為Category性質 Priority暴露出來。
  • | (或)與 & (且)在單一路徑段或屬性過濾器中結合替代方案。 將每個選項以括號包住,例如/*/*/(UnitTest1)|(UnitTest2)/*[(Category=CategoryA)|(Priority=2)]或 。

該 --treenode-filter 選項屬於 MTP 選項,必須 dotnet test 以 MTP 模式執行。 使用 .NET 10 SDK 或更新版本,請在解決方案根目錄的 global.json 檔案中新增以下設定:

{
    "test": {
        "runner": "Microsoft.Testing.Platform"
    }
}

沒有它時, dotnet test 會以 VSTest 模式執行,且在任何測試執行前就失敗 MSB1001: Unknown switch 了。 欲了解更多資訊,包括如何用早期 SDK 執行 MTP 專案,請參閱 dotnet 測試的 MTP 模式。

範例中使用以下測試類別:

namespace TUnitNamespace;

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

    [Test, Property("Priority", "2")]
    public void TestMethod2()
    {
    }
}
運算式 結果
dotnet test --treenode-filter "/*/*/*/*Method*" 執行方法名稱包含 Method的測試。
dotnet test --treenode-filter "/*/*/*/TestMethod1" 執行名為 TestMethod1的測試。
dotnet test --treenode-filter "/*/TUnitNamespace/UnitTest1/*" 負責所有考試。TUnitNamespace.UnitTest1
dotnet test --treenode-filter "/**[Category=CategoryA]" 執行以 [Category("CategoryA")] 標註的測試。
dotnet test --treenode-filter "/**[Priority=2]" 執行以 [Property("Priority", "2")] 標註的測試。

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

要執行類別中 UnitTest1且 為 Category 的 "CategoryA"測試:

dotnet test --treenode-filter "/*/*/UnitTest1/*[Category=CategoryA]"

要執行具有 a Category of "CategoryA"或 a Priority 的 "2"測試:

dotnet test --treenode-filter "/**[(Category=CategoryA)|(Priority=2)]"

要執行類別中UnitTest1且 a Category的Priority"CategoryA""2"測試:

dotnet test --treenode-filter "/*/*/UnitTest1/*[(Category=CategoryA)|(Priority=2)]"

要執行具有 a Category 的 和"CategoryA" a Priority 的"1"測試:

dotnet test --treenode-filter "/**[(Category=CategoryA)&(Priority=1)]"

另請參閱

下一步