SYSK 365: How to get your unit tests (test project in Visual Studio 2008, a.k.a. MSTest) run multithreaded

If you want to test code that requires an MTA threading model (e.g. the code being tested uses WaitHandle.WaitAll), you need to change the .testrunconfig file manually and change the threading model from STA to MTA.

 

There are other posts that say for VS 2005, you add the following line to the config file:

<apartmentState type="System.Threading.ApartmentState">

<value__ type="System.Int32">1</value__>

</apartmentState>

 

Unfortunately, same posts say that in VS 2008 you use

<ExecutionThread apartmentState="1" />

 

In my experience, it doesn’t work! Instead, you should use

          <TestRunConfiguration ...>

            ...

<ExecutionThread apartmentState="MTA" />

</TestRunConfiguration>

 

To check the threading model used, add the following line to your test(s):

 

[TestMethod]

public void YourTest()

{

System.Diagnostics.Debug.WriteLine(System.Threading.Thread.CurrentThread.GetApartmentState().ToString());

// TODO: Add test logic here

}