Easier Coverage of MSTest projects in Visual Studio 2010

We’re going to start posting tips and tricks to our blog. This is one of the first ones that we’re posting. Please let us know if there are things you’d like to see us cover in this series.

Problem:
I created a project to cover MSTest in Visual Studio 2010, but I’m not getting any coverage?

Let’s assume you have your NCover 4 project set up to monitor qtagent.exe/qtagent32.exe, or qtagent_35/qtagent32_35 (if you’re targeting .NET 3.5, specifically).

One regex pattern — “qtagent” — will capture all of these processes.

MSTest regex for QTAgent processes.

The difficulty in profiling MSTest is that QTAgent is often running in the background — NCover can’t attach to the QTAgent process once it’s already running.

Solution:
Go to the Visual Studio menu, and under Tools > Options… > Test Tools > Test Execution > Performance, uncheck “Keep test execution engine running between test runs.” This will prevent QTAgent from running in the background.

Setting Test Execution in Visual Studio Options.

Restart Visual Studio, then check Task Manager, or Process Explorer, before you start MSTest and make sure that QTAgent is not running. If it is, terminate the process so that QTAgent will start when you run MSTest in your Visual Studio project.

Run your MSTest project in Visual Studio and you should see the NCover “Modules Collecting” pop-up window display the module count as your tests are profiled.

Covering MSTest runs from Visual Studio with NCover 4 is certainly possible, but requires a little understanding of the testing environment to make coverage successful.