TestFilterProviderAttribute Class

Definition

Registers a user-supplied ITestFilter implementation that the MSTest adapter invokes for every test it is about to run, after any command-line filter (--filter, Test Explorer selection, etc.) has been applied.

[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)]
public sealed class TestFilterProviderAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)>]
type TestFilterProviderAttribute = class
    inherit 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
Public NotInheritable Class TestFilterProviderAttribute
Inherits Attribute
Inheritance
TestFilterProviderAttribute
Attributes

Examples

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

Remarks

Apply this attribute at the assembly level on the test assembly itself. At most one TestFilterProviderAttribute may be applied per test assembly; this is intentional so that filter ordering is not part of the public API. If multiple filtering strategies are needed, compose them explicitly inside a single ITestFilter implementation.

The filter type must be a non-generic class with a public parameterless constructor that implements ITestFilter. A single instance is created per test assembly per test run and reused for every test of that assembly.

The filter runs before the test type is loaded, before [AssemblyInitialize], before [ClassInitialize], and before the test constructor is invoked, so dropping or skipping a test through ITestFilter avoids paying any of those costs.

Constructors

Name Description
TestFilterProviderAttribute(Type)

Initializes a new instance of the TestFilterProviderAttribute class.

Properties

Name Description
FilterType

Gets the ITestFilter implementation registered by this attribute.

Applies to