Given this test fixture:
[TestClass] public class MSTestThreads { [TestMethod] public void Test1() { Trace.WriteLine(Thread.CurrentThread.ManagedThreadId); } [TestMethod] public void Test2() { Trace.WriteLine(Thread.CurrentThread.ManagedThreadId); } }
Running the test with MSTest through Visual Studio or command line prints two different thread numbers (yet they are run sequentially anyway).
Is there a way to force MSTest to run them using a single thread?
Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.
Cross-Platform support – V2 version of the MSTest framework is a cross-platform implementation of the framework using which developers can write tests targeting . NET Framework, . NET Core, and ASP.NET Core on platforms like Linux, Mac, and Windows.
I've fought for endless hours to make MSTest run in a single threaded mode on a large project that made heavy use of nHibernate and it's not-thread-safe (not a problem, it's just not) ISession
.
We ended up more time writing code to support the multi-threaded nature of MSTest because - to the best of my and my teams knowledge - it is not possible to run MSTest in a single threaded mode.
I solved this problem with locking:
public static class IntegrationTestsSynchronization { public static readonly object LockObject = new object(); }
[TestClass] public class ATestCaseClass { [TestInitialize] public void TestInitialize() { Monitor.Enter(IntegrationTestsSynchronization.LockObject); } [TestCleanup] public void TestCleanup() { Monitor.Exit(IntegrationTestsSynchronization.LockObject); } //test methods } // possibly other test cases
This can of course be extracted to a base test class and reused.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With