Working with NUnit Task in MSBuild Build

The information in this section is applicable if you are using NUnit prior to version 3.0. For later versions, refer to the section below.

This section assumes that you already have an MSBuild build script with a configured NUnit task in it, and want TeamCity to track test reports without making any changes to the existing build script. Otherwise, consider adding NUnit build runner as one of the steps for your build configuration.

Using NUnitTeamCity task in MSBuild Build Script

TeamCity provides a custom NUnitTeamCity task compatible with the NUnit task from MSBuild Community tasks project. If you provide the NUnitTeamCity task in your build script, TeamCity will launch its own test runner based on the options specified within the task. Thus, you do not need to have any NUnit runner, because TeamCity will run the tests.

In order to correctly use the NUnitTeamCity task, perform the following steps:

Make sure the teamcity_dotnet_nunitlauncher system property is accessible on build agents. Build agents running Windows should automatically detect these properties as environment variables. If you need to set them manually, see defining agent-specific properties for more information.

Configure your MSBuild build script with NUnitTeamCity task using the following syntax:

To make a TeamCity independent build script, consider the following trick:

<NUnitTeamCity ... Condition=" '$(TEAMCITY_VERSION)' != '' "/>

The MSBuild property TEAMCITY_VERSION is added to msbuild when started from TeamCity.

Working with NUnit 3.0

The information in this section is applicable if you are using NUnit 3.0 and above. For earlier versions of NUnit, refer to the section above.

Starting from version 3.0, NUnit supports TeamCity natively, so there is no need to use a special task for MSBuild as it was done for the earlier NUnit versions. The simplest way is to run the NUnit console via the standard Exec task. For example:

The NUnit console returns the number of failed tests as the positive exit code and, in case of the NUnit test infrastructure failure, as the negative exit code.

TeamCity controls the test execution progress, but the NUnit infrastructure exceptions may not allow TeamCity to collect the required information. That is why the IgnoreExitCode="True" attribute needs to be set, which will ignore the positive exit codes and will not interrupt the build due to several failed tests. The Error task will stop the build in case of the test infrastructure errors for the negative exit codes.

Besides the project file, you can define the MSBuild version and platform, the target, you can use profiles and other settings.