如何消除通用基础测试 class 的 UTA002/UTA005/UTA006 警告?

How do I get rid of the UTA002/UTA005/UTA006 warnings for my generic base test class?

我有一些测试 classes 做类似的事情,需要类似的初始化和清理以及类似的实例变量。因此,我创建了一个基础 class 并在其上定义了 TestInitializeTestCleanup 属性:

Public Class ImportTestBase(Of T As {ImportBase, New})
    ' ...Some instance variables and protected properties...

    <TestInitialize()>
    Public Sub Init()
        ' Connect to Test-DB, start a transaction, instantiate the import class T
    End Sub

    <TestCleanup()>
    Public Sub Cleanup()
        ' Rollback the transaction, do other cleanup stuff
    End Sub
End Class

这减少了我实际测试中的样板代码classes:

<TestClass()>
Public Class AddressImportTests
    Inherits ImportTestBase(Of MyAddresImportClass)

    <TestMethod()>
    Public Sub SomeTest()
        ' Test something here
    End Sub

    ' No boilerplate Init and Cleanup here. Yippie!
End Class

效果很好!不幸的是,Visual Studio (2015) 测试运行器输出了一些丑陋的警告:

UTA005: Illegal use of attributes on MyNamespace.ImportTestBase`1.Init.The TestInitializeAttribute can be defined only inside a class marked with the TestClass attribute.
UTA006: Illegal use of attributes on MyNamespace.ImportTestBase`1.Cleanup. The TestCleanupAttribute can be defined only inside a class marked with the TestClass attribute.

这让我很困扰,因为我不喜欢忽略警告。将 TestClass 添加到测试库 class 中无济于事;相反:

UTA002: TestClass attribute  cannot be defined on generic class MyNamespace.ImportTestBase`1.
UTA005: Illegal use of attributes on MyNamespace.ImportTestBase`1.Init.The TestInitializeAttribute can be defined only inside a class marked with the TestClass attribute.
UTA006: Illegal use of attributes on MyNamespace.ImportTestBase`1.Cleanup. The TestCleanupAttribute can be defined only inside a class marked with the TestClass attribute.

知道如何摆脱它们吗?

尝试改用 vstest 控制台。

使你的基础 class 抽象 (C#) 或 MustInherit (VB)。