5

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.

5
  • sorry, Visual Studio 2010, not 2008... I've just corrected it... Commented Nov 2, 2012 at 13:32
  • In your Initialize method, do you refer to type T at all? Commented Nov 2, 2012 at 14:17
  • No, I don't. It is not related, it just doesn't work together. Commented Nov 2, 2012 at 14:23
  • I tried without <T> and <string> . The test passes for me. Commented Nov 20, 2012 at 12:12
  • Well, thanks, but how is it supposed to help? My question was how to make it work all together - generics along with AssemlbyInitialize. Not to remove one part of it... Commented Nov 21, 2012 at 9:58

2 Answers 2

5

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()
    {
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's a nice workaround, thank you. Since I don't refer to type T in the initialize method, I don't need it present in that class, I can put it aside into some other class, like you suggested. The bug still remains there, but this workaround works for me. Thank you.
0

Does the problem still happen if you remove [TestClass] from TestClassA<T> ?

[TestClass]

 public class TestClassA<T>
 {
 }

1 Comment

Thank you for your effort. I've already tried this one, but if I remove [TestClass] attribute, the [AssemblyInitialize] method will not get called at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.