MSTest executing all my tests simultaneously breaks tests - what to do

那年仲夏 提交于 2019-12-02 19:02:58

You might want to try using a Monitor and entering in TestInitialize and exiting on TestCleanup. If your test classes all depend on the external file, you'll need to use a single lock object for all of them.

public static class LockClass
{
    public static object LockObject = new object();
}

...

[TestInitialize]
public void TestSetup()
{
     Monitor.Enter(LockClass.LockObject);
}

[TestCleanup]
public void TestCleanup()
{
     Monitor.Exit(LockClass.LockObject);
}

This should force all of your tests to run serially and as long as all of your tests pass/fail they should run. If any of them throws an unexpected exception, though, all the rest will hang since the Exit code won't be run for the test that blows up.

Seven

I had a try using locks in this manner. What I experienced, however, was that VS2010 does not execute the tests in parallel by default, but executes them sequencially, in a single thread. (parallel execution could be switched on, however. But this would not prevent the problem completely)

What I find very disturbing is, that the sequencial execution will take place in arbitrary order, even across test classes!

So for example an execution order may look like this:

  • Class A - TestInitialize: Lock will be established
  • Class A - TestMethod1: Will execute, OK
  • Class B - TestInitialize: Lock will be established => Thread will be blocked => Complete UnitTests will be blocked! The cause is that there are no other Threads which would go on executing methods of Class A. So the Montor.Exit() will never be reached.

I do not understand why MS is doing so. Other UnitTest frameworks (e.g. JUnit) execute the test methods class-wise. Otherwise there will be some interleaving of SetUp/TearDown method which would cause the chaos described...

Is there anybody out there knowing how to prevent MSTest jumping between test classes? (Currently I use Resharpers test runner, which behaves as expected, executing all tests methods of one classe before proceeding with the next class)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!