I have a few test classes that do similar things, need similar initialization and cleanup and similar instance variables. Thus, I created a base class and defined the TestInitialize
and TestCleanup
attributes on them:
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
This reduces boilerplate code in my actual test 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
This works great! Unfortunately, the Visual Studio (2015) test runner outputs some ugly warnings:
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.
This bothers me, because I don't like to ignore warnings. Adding TestClass
to the test base class does not help; on the contrary:
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.
Any idea on how to get rid of them?
Make your base class abstract (C#) or MustInherit (VB).