Integrate NUnit in Hudson CI-server

Precondition Naming Its a good practice to have a common naming schema for test projects, fixture categories and fixture. Especially to configure automatic r...

Precondition

Naming

Its a good practice to have a common naming schema for test projects, fixture categories and fixture. Especially to configure automatic runs of unit tests is much more easier with this.

For each project we have a correspondending test project which has the same name suffixed with .Test. Other teams uses .Test, .Fixture or .Fixtures.

Fixtures themself have the same name like the to be tested class suffixed with Fixture. Maybe that this is not the best idea, because normally each concern of a class should have an own fixture. So the realtionship between class and fixture is not 1:1 but 1:n.

Often you have the situation, that you wanna have additional tests which are not classic unit tests, but test which makes sense for you or functionally steps which you wanna start from your IDE. These tests are marked with a special category – i use the term NoContinuousIntegrationTest.

So this looks like:

namespace EsriDE.PipelineManagement.Core.Test

{

    [TestFixture]

    public class ToolboxConfigurationFixture : PipelineManagementFixtureBase

    {        ..

        [Test]

        [Category("NoContinuousIntegrationTest")]

        public void ReadingToolboxConfigurationWorks()

Common output folder for projects

For using IoC-frameworks is often a good idea to have a common output folder for all builds. This common output folder also offers us the possibility to run tests in only one folder. Our common output folder is a sibling to the source folder.

Content of the VCS

Needless to write that you should include the NUnit stuff in your VCS. In our case the files are located beneath .\build\NUnit

The functional part

Creating a batch to run NUnit console runner

Mostly you need the possibility to run unit test outside your IDE. NUnit offers a GUI and a console runner for this. GUI could be nice for you, console runner is nice for build streps. ;-)

To allow a simple access for the console runner you should create a batch in your source root folder. RunNUnitConsole.bat is a good name for this batch.

Content

IF "%1" == "" (SET Configuration=Debug) ELSE (SET Configuration=%1)
ECHO Configuration=%Configuration%
PUSHD .\..\bin
DEL *.dll.NUnitResult.xml
POPD
PUSHD .\..\bin\%Configuration%
FOR %%f IN (*.Test.dll) DO .\..\..\build\Nunit\nunit-console-x86.exe %%f /process=Single /domain=Single /nothread /exclude="NoContinuousIntegrationTest" /xml=.\..\%%f.NUnitResult.xml
POPD

Explanation

The first section we need later for including in Hudson. Parameter %1 and varaible %Configuration% lets distinguish between Debug- and Release-Build. If the batch is called without an argument the Debug-Build is the default build.

Section two cleans former unit test run results.

The realy interesting part is the last section. First we switch in the above explained common output folder – depending on the build-configuration.

Next we interate through all assemblies which names and with .Test.dll and using this assemblies as an input for the nunut console runner (nunit-console-x86.exe).

  • parameters /process=Single /domain=Single /nothread

    Special configuration for our environment.

  • /exclude=“NoContinuousIntegrationTest”

    Does not consider tests which are marked as the categorie “NoContinuousIntegrationTest”

  • /xml=...%%f.NUnitResult.xml

    Outputs for each test project a XML-based result file – named like the project suffixed with .NUnitResult.xml

Configuring Hudson

Now its the right moment to configure your Hudson job

Run the batch

Add build step to your Hudson job (Execute Windows batch command) with the following command:

PUSHD ".\trunk\src\"
RunNUnitConsole.bat %CONFIGURATION%
POPD
ECHO ERRORLEVEL FROM UNIT-TESTING IS %ERRORLEVEL%

The argument %CONFIGURATION% is managed from Hudson.

Configuring Dashboard

The last step is the functionallity to collect the unit test results for Hudsons dashboard.

Maybe you must install a special plugin – i do not remember that.

Activate “Publish NUnit test result report” and specify **/*/*NUnitResult.xml

The next job execution lets run our batch with the nunit console runner over all test assemblies and after that the dashboards shows a statistic with the results.