Thursday, February 23, 2012

Running Unit Tests in Parallel

Its been possible to run unit tests in parallel for some time now, but it doesn't seem to have taken off. NUnit has had a parallel runner (PNUnit) since 2007, MsTest since 2009. I think running tests in parallel has two distinct benefits:
  1. The test run will complete a little sooner.
  2. It will help to test your system for concurrency issues.
HOWEVER, all your tests must be thread-safe. Writing unit tests that can be run in parallel can be difficult and designing and writing a system that allows parallel unit testing can be even harder. Statics and singletons will be the undoing.

So how do you set up tests to run in parallel?

For MsTest you will need to edit the .TestSettings file with a text editor (or right click in Visual Studio and open with Xml Editor).

Set the number to the number of cores you want to use for running tests. Setting this to a number greater than cores you have in your system will have no benefit. Setting it to 0 will auto-configure and use as many cores as the runner can. Remove the attribute completely to disable.

This will then ensure running the tests from within Visual Studio's Test menu run in parallel. (Make sure the testsettings file you modified is selected as the current active settings file, do this in the Test menu). If you're using Resharper I'm not certain it honour's all the settings in the testsettings file. (I didn't notice any difference with parallel enabled).

Setting it up for NUnit is a bit easier, it simply means you use a different runner. The bad news is that its command-line only with no integration into Visual Studio. See PNUnit for details on downloading the runner.

No comments:

Post a Comment