I have a problem with unit tests in Visual Studio 2010. I've pasted the simplified code below.
[TestClass]
public class TestClassA<T>
{
[AssemblyInitialize()]
public static void Initialize(TestContext testContext) {}
}
[TestClass]
public class TestClassB : TestClassA<String>
{
[TestMethod]
public void TestMethod()
{
Assert.IsTrue(true);
}
}
When I run TestMethod()
, I get the following exception:
Assembly Initialization method TestProject.TestClassA`1.Initialize threw exception. System.InvalidOperationException: System.InvalidOperationException: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.. Aborting test execution.
at System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException()
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.RunAssemblyInitializeMethod()
When I google this bug, I can find advice how to fix code that uses reflection to call the [AssemblyInitialize] method. But that code is not mine, it's:
Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.RunAssemblyInitializeMethod()
I can use [ClassInitialize] method instead of [AssemblyInitialize] and it works, but still I would like to use [AssemblyInitialize] method.
Thank you in advance for any suggustions.
You don't actually need the inheritance. You can put a separate class in your test project/assembly that contains both AssemblyInit and AssemblyCleanUp attributed methods. Like so:
[TestClass]
public static class AssemblyInitializer
{
[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
}
[AssemblyCleanup]
public static void AssemblyCleanup()
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With