nunitteamcityncovertypemocktypemock-isolator

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


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:

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.


Solution

  • 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.