views:

176

answers:

1

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?

+2  A: 

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.

Ohad Horesh