Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Ok, this is annoying.

MSTest executes all of my tests simultaneously which causes some of them to fail. No this is not because my tests are fragile and susceptible to build order rather it is because this is a demo project in which I use a Db4o object database running from a file.

So I have a couple of DataAccess tests checking that my repositories work correctly and boom, MSTest blows up. Since it tries to run all its tests at the same time it gets an error when a test tries to access the database file while other tests are using it.

Can anyone think of a quick way around this? I don't want to ditch MSTest (ok I do but another story) and I sure as heck don't want to run a full-blown database service so I'll take any way to force MSTest not to run simultaneously or tricks with opening files.

Anyone have any ideas?

like image 622
George Mauer Avatar asked Mar 31 '09 04:03

George Mauer


People also ask

Does MSTest run tests in parallel?

The biggest advantage of MSTest is that it allows parallelization at the method level. As opposed to the other frameworks which only allow parallelization at the class level. So, for example, If you have 100 test methods in 5 classes, MSTest will let you run 100 tests in parallel.

How do I run parallel test cases in Visual Studio?

Visual Studio 2019 runs unit tests sequentially when it is suppose to run them in parallel. Click on the Run Tests in Parallel button in Test Explorer. Make sure the icon is highlighted. Run unit tests.

Does NUnit run tests in parallel?

The NUnit 3.0 framework can run tests in parallel within an assembly. This is a completely separate facility from Engine Parallel Test Execution, although it is possible to use both in the same test run. By default, no parallel execution takes place.

What is MSTestV2?

Uniform app-platform support – MSTestV2 is a converged implementation that offers uniform app-platform support across . NET Framework, . NET Core, ASP.NET Core, and UWP. Read more. The implementation is fully cross platform (Windows, Linux, Mac).


1 Answers

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.

like image 181
tvanfosson Avatar answered Sep 28 '22 02:09

tvanfosson