Język

TestFilterProviderAttribute Klasa

Definicja

Rejestruje implementację dostarczoną przez ITestFilter użytkownika, którą karta MSTest wywołuje dla każdego testu, który ma zostać uruchomiony, po zastosowaniu filtru wiersza polecenia (--filterwyboru Eksploratora testów itp.).

[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
Dziedziczenie
TestFilterProviderAttribute
Atrybuty

Przykłady

[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;
}

Uwagi

Zastosuj ten atrybut na poziomie zestawu na samym zestawie testowym. Co najwyżej jeden TestFilterProviderAttribute może być stosowany dla zestawu testowego. Jest to celowe, aby kolejność filtrów nie jest częścią publicznego interfejsu API. Jeśli potrzebujesz wielu strategii filtrowania, utwórz je jawnie w ramach jednej ITestFilter implementacji.

Typ filtru musi być niegeneryczny i wystąpienia — betonowa klasa lub struktura, ale nie — z publicznym konstruktorem ref struct bez parametrów i musi zaimplementować ITestFilterelement . Pojedyncze wystąpienie jest tworzone na zestaw testowy na przebieg testu i ponownie używane dla każdego testu tego zestawu.

Ponieważ typ filtru jest przekazywany jako Typeelement , żaden z tych wymagań nie jest sprawdzany przez kompilator. Analizator MSTEST0081 weryfikuje je w czasie kompilacji. Na .NET wariant ogólny TestFilterProviderAttribute<TFilter> dodatkowo wymusza ITestFilter wymagania konstruktora bez parametrów publicznych za pomocą ogólnych ograniczeń; pozostałe są nadal zgłaszane tylko przez MSTEST0081.

Filtr jest uruchamiany przed załadowaniem typu testu, przed [AssemblyInitialize], i przed [ClassInitialize]wywołaniem konstruktora testowego, więc upuszczanie lub pomijanie testu przez ITestFilter pozwala uniknąć płacenia któregokolwiek z tych kosztów.

Konstruktory

Nazwa Opis
TestFilterProviderAttribute(Type)

Inicjuje nowe wystąpienie klasy TestFilterProviderAttribute.

Właściwości

Nazwa Opis
FilterType

Pobiera implementację zarejestrowaną ITestFilter przez ten atrybut.

Dotyczy