语言

TestFilterProviderAttribute<TFilter> 类

定义

该强类型变体 TestFilterProviderAttribute 注册 TFilter 为 ITestFilter MSTest 适配器针对要运行的每个测试调用的实现。

[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)]
[System.Diagnostics.CodeAnalysis.Experimental("MSTESTEXP", UrlFormat="https://aka.ms/mstest/diagnostics#{0}")]
public sealed class TestFilterProviderAttribute<TFilter> : Attribute where TFilter : ITestFilter, new()
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=false, Inherited=false)>]
[<System.Diagnostics.CodeAnalysis.Experimental("MSTESTEXP", UrlFormat="https://aka.ms/mstest/diagnostics#{0}")>]
type TestFilterProviderAttribute<'Filter (requires 'Filter :> ITestFilter and 'Filter : (new : unit -> 'Filter))> = class
    inherit Attribute
Public NotInheritable Class TestFilterProviderAttribute(Of TFilter)
Inherits Attribute

类型参数

TFilter

用于 ITestFilter 实例化和调用使用测试程序集中每个测试的实现。

继承
TestFilterProviderAttribute<TFilter>
属性

示例

[assembly: TestFilterProvider<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;
}

注解

这是类型安全的形式 [assembly: TestFilterProvider(typeof(MyFilter))]。 泛型约束使编译器在生成时强制实施非泛型属性只能在运行时验证的内容: TFilter 必须实现 ITestFilter 并公开公共无参数构造函数。

其他所有属性的行为完全相同 TestFilterProviderAttribute,包括“最多每个测试程序集一个”规则 , 这两个属性计数为一个和相同的注册,因此将两个属性都应用于单个程序集是一个错误。 它们是不同的类型,因此编译器不会将其报告为重复属性; MSTEST0081 执行操作。

仅当面向.NET时,此属性才可用。 在 .NET Framework 上,使用非泛型 TestFilterProviderAttribute。 多目标两者都可以使用以下 #if NET方式选择正确的窗体的测试项目:

#if NET
[assembly: TestFilterProvider<MyFilter>]
#else
[assembly: TestFilterProvider(typeof(MyFilter))]
#endif

构造函数

名称 说明
TestFilterProviderAttribute<TFilter>()

该强类型变体 TestFilterProviderAttribute 注册 TFilter 为 ITestFilter MSTest 适配器针对要运行的每个测试调用的实现。

适用于