Working around the AnyCPU Limitation with Visual Studio Unit Tests

When running unit tests in Visual Studio 2010, you can run them in 64 bit mode, however, the assembly containing the tests MUST be compiled as AnyCPU.  This is because the test discovery process happens inside of Visual Studio (or inside  MSTest.exe if you are using the command line).  Since both of these applications are 32 bit only, they cannot load the 64 bit assembly (unless it is compiled as AnyCPU) and consequently, you cannot run your unit tests.

Fortunately, thanks to the fact that both Visual Studio and MSTest.exe only need to discover these test and the way JIT’ing works, you can work around this problem.  As a work-around, you can create a stub test assembly (ANYCPU) that will reference the 64 bit only assembly.  This stub test assembly should contain all of the test methods that you want enumerated in the Visual Studio or MSTest.exe, but would not call any methods from a 64 bit only assembly directly.  Instead, create a another method to call the methods that are inside of the 64 bit only assemblies.  Because these methods will not be JIT’d until the test is actually run, this will allow the test methods to be enumerated by a 32 bit process, which in turn will allow you to run your 64 bit only tests.

For Example:

private void DoTest1()

{

                SixtyFourBitClass.MethodToTest();

}

[TestMethod]

public void Test1()

{

                DoTest1();

}