我正在尝试使用 Microsoft Shims 运行此单元测试,但它在 Shims.Context.Create();
方法中抛出异常。
环境:VS 2012,Win2K8 R2
namespace MyShimsUnitTest
{
[TestClass]
public class MyUnitTest
{
[TestMethod]
public void GetCurrentYear()
{
using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create())
{
// Some Logic...
}
}
}
}
详细异常:
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()
最佳答案
垫片需要由 IntelliTrace 探查器执行的运行时检测。测试运行器负责设置 CLR 加载探查器所需的环境变量,并提供探查器必须为 Shims 检测的类型列表。当 ShimRuntime 无法找到并附加到 IntelliTrace 探查器时,将抛出 UnitTestIsolationException,它预计 CLR 已经加载了它。
正如 Jin-Wook 之前提到的,当测试由未执行所需的探查器初始化的运行器执行时,就会出现此问题。 Test Explorer 和 vstest.console.exe 是 Visual Studio 2012 附带的两个这样的运行器。此时,Visual Studio 测试运行器在以“传统”模式运行测试时不执行所需的探查器检测,这发生在您有为您的运行选择的 .TESTSETTINGS 文件或强制使用传统模式的 .RUNSETTINGS 文件。
您可以使用支持 Shims 所需的分析器检测的第三方测试运行器。
关于c# - 单元测试隔离异常 : Throws Exception while running Shims test in Release/Debug mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16664814/