NUnit, and NCover to work
together in your build script can sometimes be a tricky thing. Getting
any one of those things to work individually is easy enough, getting two
going is a little tougher, but getting all three together requires a bit
of finesse. Add to that the fact that you may run different versions of
different products (different NUnit versions, for example) for different
source code bases and it gets downright complicated.
The way my product code works, when I check out the codeline I want to
work on it comes with all of the dependencies - every third party
assembly, every build tool. That includes TypeMock, NUnit, and NCover.
That way the build server doesn’t have to have anything installed when
it runs a build - it can auto-deploy TypeMock, register NCover, do its
thing, and undo all of that when it’s done. (Yes, there are some
drawbacks to that - parallel builds are limited when registered versions
of profilers change, for example - but we’ve dealt with that sort of
thing in other ways.)
On developer workstations, we have TypeMock installed so we can make use
of the tracer and other helpful utilities, but on the build server, we
Since TypeMock is a profiler, if you use it in your unit tests, you
can’t just run NUnit in the build and have it work - you have to start
TypeMock, then run NUnit, then shut TypeMock back down. If you’re using
NCover, you have to make sure NCover is registered and linked with
TypeMock comes with some custom build tasks to help you get this
working. You will also want to get NCoverExplorer and
to get this working well. NCoverExplorer will aggregate coverage logs
for you and NCoverExplorer.Extras comes with NCover and NCoverExplorer
The general flow of what needs to happen is this:
- Register TypeMock with the system (if it’s not a developer
workstation - our devs have it installed).
- Register NCover with the system.
- Start TypeMock and link it with NCover.
- Run NCover and pass it the command line parameters to run your NUnit
tests. Tell it which assemblies to profile.
- Stop TypeMock.
- Unregister NCover with the system.
- Use NCoverExplorer to aggregate the coverage reports into a single
- On error, stop TypeMock and unregister NCover.
Here’s the example:
<?xml version="1.0" encoding="utf-8" ?>
<!-- Register the tasks necessary for running tests and coverage. -->
<!-- Property indicating we're building on a dev machine - build server will set this to false. -->
<!-- Property indicating where build logs will go. -->
<!-- Register TypeMock only if it's the build server - it'll already be on a developer box. -->
<!-- Register NCover so it's available for TypeMock. -->
<Exec Command="regsvr32 /s "Relative\Path\To\NCover\CoverLib.dll""/>
<!-- Start TypeMock and link it with NCover. -->
<!-- Enumerate the test assemblies you'll be executing with NUnit. -->
<Output TaskParameter="Include" ItemName="UnitTestAssemblies"/>
<!-- Create the folder where unit test and coverage logs will go. -->
<!-- Run NUnit through NCover so profiling happens. -->
<!-- Note the use of "batching" so this is equivalent to a "foreach" loop in MSBuild. -->
<!-- Stop TypeMock and unregister NCover. -->
<!-- Get all of the coverage logs and aggregate them with NCoverExplorer. -->
<Output TaskParameter="Include" ItemName="CoverageReports"/>
<!-- In case one of the tests fails, make sure to stop TypeMock and unregister NCover. -->
<!-- Stopping TypeMock and unregistering NCover is a separate target because it has to happen -->
<!-- regardless of success or failure of the unit tests. Like the "finally" in a "try/finally" block. -->
<Exec Command="regsvr32 /u /s "Relative\Path\To\NCover\CoverLib.dll"" ContinueOnError="true"/>
In the example, notice how the steps for stopping TypeMock and
unregistering NCover have been placed in a separate target called
“test-finally” since it’s used a lot like a try/finally block. That’s
the sort of thing we’re trying to emulate. You’ll also notice that we’re
run each test assembly through NCover and generate an individual
- Obviously you’re going to need to change the paths and other
placeholder parameters to fit your build.
- If you’ve got TypeMock installed on dev machines and on the build
box, you can skip the TypeMockRegister task and just start/stop
- If you’ve got NCover installed on dev machines and on the build box,
you don’t need to execute “regsvr32” to register/unregister NCover.
As long as NCover is registered before you start TypeMock, you’re