我有一些类似的测试类,需要类似的初始化和清理以及类似的实例变量。因此,我创建了一个基类,并在其上定义了TestInitialize
和TestCleanup
属性:
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
这减少了我实际测试类中的样板代码:
<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
添加到测试基类没有帮助;恰恰相反:
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.
关于如何摆脱它们的任何想法?
答案 0 :(得分:0)
将基类抽象为(C#)或MustInherit(VB)。
答案 1 :(得分:-1)
请尝试使用vstest控制台。