UnitTestIsolationException: Throws Exception while running Shims test in Release/Debug mode

Santosh Panda picture Santosh Panda · May 21, 2013 · Viewed 7.7k times · Source

I am trying to run this unit test using Microsoft Shims, but it throws me exception in Shims.Context.Create(); method.

Environment: VS 2012, Win2K8 R2

namespace MyShimsUnitTest
{
    [TestClass]
    public class MyUnitTest
    {
        [TestMethod]
        public void GetCurrentYear()
        {
            using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create())
            {
                // Some Logic...
            }
        }

    }
}

Detailed Exception:

Result Message: 
Test method MyShimsUnitTest.MyUnitTest.GetCurrentYear threw exception: 
Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException: UnitTestIsolation instrumentation failed to initialize. Please restart Visual Studio and rerun this test
Result StackTrace:  
at Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InitializeUnitTestIsolationInstrumentationProvider()
   at Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.CreateContext()
   at Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()
   at MyShimsUnitTest.MyUnitTest.GetCurrentYear()

Answer

Oleg Sych picture Oleg Sych · Jun 14, 2013

Shims require runtime instrumentation performed by the IntelliTrace profiler. The test runner is responsible for setting up the environment variables required for CLR to load the profiler as well as providing the list of types the profiler must instrument for Shims. The UnitTestIsolationException is thrown when the ShimRuntime is unable to locate and attach to the IntelliTrace profiler, which it expects to be already loaded by the CLR.

As Jin-Wook mentioned earlier, this problem occurs when the test is executed by a runner that does not perform the required profiler initialization. Test Explorer and the vstest.console.exe are two such runners that ship with Visual Studio 2012. At this time, the Visual Studio test runners do not perform the required profiler instrumentation when running tests in "legacy" mode, which happens when you have a .TESTSETTINGS file selected for your run or a .RUNSETTINGS file that forces legacy mode.

You may be able to use third-party test runners that support profiler instrumentation required by Shims.