使用 TypeMock Isolator.Swap.AllInstances在 Visual Studio 负载测试中?
我有以下测试设置。
[TestClass,
Isolated]
public class TestClass
{
public TestClass()
{
}
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[ClassInitialize,
Isolated]
public static void InitializeRunState(TestContext testContext)
{
Helpers.SetupMocks();
//do some class init stuff
}
[TestInitialize]
public void InitializeTestState()
{
Helpers.SetupMocks();
}
[TestMethod]
public void Test()
{
//execute test
}
}
在 Helpers.SetupMocks()
方法中,我正在调用 Isolator.Swap.AllInstances
。
只要我不执行负载测试,这就非常有效。 一旦我配置了一个将执行 Test
方法的负载测试,TypeMock 就会开始抛出此异常:
TypeMock.TypeMockException:*** 无法对某个类型多次调用 Swap.AllInstances()。
有什么办法可以避免这种情况吗? 我是否配置错误?
I have the following tests setup.
[TestClass,
Isolated]
public class TestClass
{
public TestClass()
{
}
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[ClassInitialize,
Isolated]
public static void InitializeRunState(TestContext testContext)
{
Helpers.SetupMocks();
//do some class init stuff
}
[TestInitialize]
public void InitializeTestState()
{
Helpers.SetupMocks();
}
[TestMethod]
public void Test()
{
//execute test
}
}
In Helpers.SetupMocks()
method I am making a call to Isolator.Swap.AllInstances<T>()
.
This works great as long as I'm not executing a load test. As soon I configure a load test that will execute the Test
method TypeMock starts throwing this exception:
TypeMock.TypeMockException: *** Can not call Swap.AllInstances() more than once on a type.
Is there anyway to avoid this? Do I have something configured wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
免责声明 我在 Typemock 工作
首先请注意,我们抛出此异常是因为多次伪造一个类型的所有实例确实没有意义,我们想让用户知道他可能犯了一个错误。
我认为问题在于,当您运行负载测试时,mstest 并行运行测试类的几个实例。
在这种情况下,您应该将对 Isolator.Swap.AllInstances() 的调用从类设置移至测试方法。
如果 mstest 运行不会在同一类中并行运行测试,那么它可能会起作用。
不幸的是,mstest 似乎没有命令行参数来覆盖此行为。
Disclaimer I work at Typemock
First note that we throw this exception because there's really no point in faking all instances of a type more than once and we want to let the user know that he probably made a mistake.
I think the problem is that when you run the load tests mstest runs few instances of the test class in parallel.
In that case you should move the call to Isolator.Swap.AllInstances() from the class setup to the test methods.
It might work if mstest runs will not run the tests in parallel in the same class.
Unfortunately it seems that mstest does not have a command line argument for overriding this behavior.