語言

TestFilterProviderAttribute 類別

定義

註冊使用者提供的 ITestFilter 實作,MSTest 介面卡會在每次即將執行的測試時呼叫該實作,且在已套用任何命令列過濾器(--filter如測試總管選擇等)後。

[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)]
[System.Diagnostics.CodeAnalysis.Experimental("MSTESTEXP", UrlFormat="https://aka.ms/mstest/diagnostics#{0}")]
[System.Runtime.CompilerServices.Nullable(0)]
public sealed class TestFilterProviderAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)]
public sealed class TestFilterProviderAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)>]
[<System.Diagnostics.CodeAnalysis.Experimental("MSTESTEXP", UrlFormat="https://aka.ms/mstest/diagnostics#{0}")>]
[<System.Runtime.CompilerServices.Nullable(0)>]
type TestFilterProviderAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)>]
type TestFilterProviderAttribute = class
    inherit Attribute
Public NotInheritable Class TestFilterProviderAttribute
Inherits Attribute
繼承
TestFilterProviderAttribute
屬性

範例

[assembly: TestFilterProvider(typeof(NightlyFilter))]

public sealed class NightlyFilter : ITestFilter
{
    public TestFilterResult Filter(TestFilterContext context)
        => context.Categories.Contains("Nightly")
            && Environment.GetEnvironmentVariable("RUN_NIGHTLY") != "1"
            ? TestFilterResult.Skip("Set RUN_NIGHTLY=1 to run nightly tests.")
            : TestFilterResult.Run;
}

備註

在測試組件本身的組裝層級套用此屬性。 每個測試組合最多只能套用一個 TestFilterProviderAttribute ;這是刻意設計,避免過濾排序成為公開 API 的一部分。 若需多種過濾策略,則可明確組合於單一 ITestFilter 實作中。

濾波器型別必須是非通用且可即時化的——一個具體類別或結構體,但不能 ref struct 是具有公開無參數建構器的 —,且必須實作 ITestFilter。 每個測試組件每次測試執行都會建立一個實例,並重複使用該組件的每個測試。

由於濾波器類型以 傳遞 Type為 ,編譯器不會檢查這些要求。 MSTEST0081分析器會在建置時驗證它們。 在 .NET 上,泛型TestFilterProviderAttribute<TFilter>變體還會透過其泛型約束強制執行 ITestFilter 和 公有參數無參數建構子的需求;其餘限制仍僅由 報告。MSTEST0081

過濾器在測試類型載入[AssemblyInitialize]、在 、 [ClassInitialize]之前,以及在測試建構子被呼叫之前運行,因此放棄或跳過ITestFilter測試可避免支付這些成本。

建構函式

名稱 Description
TestFilterProviderAttribute(Type)

初始化 TestFilterProviderAttribute 類別的新執行個體。

屬性

名稱 Description
FilterType

會以此屬性註冊實作。ITestFilter

適用於