Getting TeamCity to run NUnit tests, with both NCover and TypeMock integrated?

1.5k views Asked by At

Basically I'd like to use the NUnit plugin for TeamCity (the program, not necessarily the specific build step using it) to run my unit tests, with NCover for code coverage, and since my unit tests uses TypeMock 6, I need that too working.

So far I've tried:

  • Just basically pointing the TeamCity NUnit build-step to my dll's, but that fails with the following error message:

    Typemock Isolator needs to be linked with Coverage Tool to run, to enable do one of the following:

    1. link the Coverage tool through the Typemock Isolator Configuration
    2. run tests via TMockRunner.exe -link
    3. use TypeMockStart tasks for MSBuild or NAnt with Link
  • Trying to figure out the right command line, I tried this:

    C:...\Isolator\6.0\TMockRunner.exe "C:\TeamCity...\JetBrains.BuildServer.NUnitLauncher.exe" v4.0 MSIL NUnit-2.5.9 MyAssembly.dll

    This fails with the exact same error.

  • Setting the environment variables found in the mocking_on.bat file part of TypeMock, this doesn't change the outcome.

Note that the above examples doesn't contain any reference to NCover (yet), that's because I've been hacking around on the command line for a couple of hours with the above examples and still haven't gotten basic unit-tests running. NCover is extra options to the nunit-launcher of TeamCity so I hope this is as simple as just enabling that when I get that far.

2

There are 2 answers

0
Lasse V. Karlsen On BEST ANSWER

Since TypeMock requires you to use their own runner program, TMockRunner, there's no way to just use the GUI options in TeamCity to get everything set up.

Instead, what I ended up doing was to first build a custom msbuild file with this contents:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <TypeMockLocation>C:\Program Files (x86)\TypeMock\Isolator\6.0</TypeMockLocation>
        <NUnit>"C:\TeamCity\buildAgent\plugins\dotnetPlugin\bin\JetBrains.BuildServer.NUnitLauncher.exe"</NUnit>
        <NCover>C:\Program Files (x86)\NCover\NCover.Console.exe</NCover>
    </PropertyGroup>
    <Import Project="$(TypeMockLocation)\TypeMock.MSBuild.Tasks"/>
    <Target Name="TestWithTypeMock">
        <TypeMockStart Link="NCover3.0" ProfilerLaunchedFirst="true" Target="2.0"/>
            <Exec ContinueOnError="true" Command="$(NUnit) v2.0 x86 NUnit-2.5.9 SqlDatabases.Core.Tests\bin\Debug\SqlDatabases.Core.Tests.dll SqlDatabases.SqlServer.Tests\bin\Debug\SqlDatabases.SqlServer.Tests.dll /ncover:%22$(NCover)%22 /ncover-arg://ias /ncover-arg:SqlDatabases.Core /ncover-arg://ias /ncover-arg:SqlDatabases.SqlServer /ncover-arg://et /ncover-arg:.*Exception /ncover-arg://x /ncover-arg:c:\temp\coverage.xml"/>
        <TypeMockStop/>
    </Target>
</Project>

This file I save to a directory on my TeamCity server. Since I didn't want the test script to be part of my repository, I didn't add it to source control (I can build and right-click and run tests from within Visual Studio, if I get something not so tied to my build server I might change that decision later). Also, I only have 1 build-agent for my TeamCity server so this works for me for the time being.

In addition to the above file, I added the following batch-file:

@echo off
setlocal
set CURDIR=%CD%
copy c:\dev\sqldatabases\tests.msbuild .\
msbuild tests.msbuild /target:TestWithTypeMock
rd /s /q c:\dev\sqldatabases\codecoverage
md c:\dev\sqldatabases\codecoverage
"c:\program files\ncover\ncover.reporting.exe" c:\temp\coverage.xml //or FullCoverageReport:Html:c:\dev\sqldatabases\codecoverage
cd \dev\sqldatabases\codecoverage
del %CURDIR%\coverage.zip
7z a -r %CURDIR%\coverage.zip

And then I added the following two build-steps to my TeamCity build configuration:

  1. Visual Studio Solution: Build the debug configuration.
  2. Execute C:\Dev\SqlDatabases\Tests.bat (the above batch file)

I made sure that coverage.zip was listed under artifacts in my build configuration, and now I got code coverage, typemock, and unit tests.

Only thing I haven't figured out how to get so far is that if I click on the build result of a build configuration that uses the normal TeamCity GUI for setting up everything (minus TypeMock), I get a code coverage short summary listed, this is not present in the above setup, but the full report is, and the tab in the TeamCity build results.

Hopefully this can save someone else some trouble.

1
IronQuill On

Lasse,

So long as you're using the TeamCity GUI, running with NCover coverage should just be a simple matter of selecting to run with it.I've never tried throwing Typemock into the mix, so I look forward to hearing how that goes.