Previously I have written about using OpenCover with xUnit. It struck me that I should probably also do the same for nUnit as that is what we use in PodcastUtilities

The structure of these scripts is slightly different from the xUnit scripts as they have to work both 16 and 32 bit.

The master script looks like this

call SetProgramFilesEnv.bat
if "%ProgramFilesPath%" == "" exit 1
set opencovercmd="%LocalAppData%\Apps\OpenCover\OpenCover.Console.exe"
set nunitcmd="%ProgramFilesPath%\NUnit 2.6\bin\nunit-console-x86.exe"
%opencovercmd% -register:user -target:%nunitcmd% -targetargs:"/noshadow /apartment:mta PodcastUtilities.nunit" -targetdir:"." -output:coverage.xml -filter:"+[PodcastUtilities.Common]* +[PodcastUtilities.Presentation]* -[PodcastUtilities.Common]PodcastUtilities.Common.Platform* -[PodcastUtilities.Common]PodcastUtilities.Common.Exceptions*"
rem this is a generator available from
.\Tools\ReportGenerator\ReportGenerator.exe coverage.xml .\Coverage

OpenCover installs into the %LocalAppData% path which does make writing scripts easy. Once again I have used the /noshadow option just for speed

If I did not specify the /apartment option then I got the following error as by default it attempts to run the tests in the STA and the PodcastUtility tests need to be run in the MTA

1) SetUp Error : PodcastUtilities.Common.Tests.TaskPoolTests.WhenATaskCompletesAndThereAreStillTasksLeftToRun.ItShouldStartTheNextTask
   SetUp : System.NotSupportedException : WaitAll for multiple handles on a STA thread is not supported.
   at System.Threading.WaitHandle.WaitMultiple(WaitHandle[] waitHandles, Int32 millisecondsTimeout, Boolean exitContext, Boolean WaitAll)

To accommodate 32 bit and 64 bit users the SetProgramFilesEnv.bat sets up the correct path like this

set ProgramFilesPath=

if not "%PROGRAMFILES(x86)%" == "" goto win64
if not "%ProgramFiles%" == "" goto win32
echo Cannot find program files environment variable
goto end

set ProgramFilesPath=%ProgramFiles%
goto end

set ProgramFilesPath=%PROGRAMFILES(x86)%


The excellent ReportGenerator is “installed” (unzipped) in a sub folder