Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Azure, PHP, OpenXML, VSTS, ...

About the author

Maarten Balliauw is an MVP ASP.NET and is currently employed as .NET Software Engineer at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP.
More about me More about me
Send mail E-mail me


Microsoft Most Valuable Professional - MVP - ASP.NET

Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn RealDolmen - Rock-solid passion for ICT
I'm a speaker at TechDays Belgium and TechDays Finland

Search

Latest Twitter

    Follow me on Twitter...

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2010

    Integrating NUnit test results in Team Build 2008

    When using Team Foundation Server 2008 and Team Build, chances are you are developing unit tests in Microsoft’s test framework which is integrated with Visual Studio 2008. This integration offers valuable data hen a build has been finished on the build server: test run results are published in the Team Foundation Server 2008 data warehouse and can be used to create detailed metrics on how your development team is performing and what the quality of the product being developed is.

    Not all software development teams are using Microsoft’s test framework. Perhaps your team is using Team Foundation Server 2008 and creates (unit) tests using NUnit. By default, NUnit tests are not executed by the Team Build server nor are they published in the Team Foundation Server 2008 data warehouse. The following guide enables you to leverage the features Team Foundation Server 2008 has to offer regarding metrics, by customizing the build process with the necessary steps to publish test results.

    (cross-posted on RealDolmen ALM Blog)

    1. Prerequisites

    Make sure the following prerequisites are present on your Team Build server (in addition to a default build server installation):

    2. Registering NUnit framework in the global assembly cache (GAC)

    For NUnit tests to be run in a Team Build script, make sure that the NUnit framework is registered in the global assembly cache (GAC). This can be achieved by copying the file C:\Program Files\NUnit 2.4.8\bin\nunit.framework.dll to C:\Windows\Assembly.

    clip_image002

    3. Customizing a build script

    After installing all prerequisites, make sure you know all paths where these tools are installed before continuing.

    The build script for a NUnit enabled build should be modified in several locations. First of all, the MSBuild Community Tasks target file should be referenced. Next, a new build step is added in the AfterCompile hook of the build script. This build step will run the NUnit tests in the compiled DLL’s, transform them to a Microsoft Test results file (*.trx) and publish this transformed file to the Team Foundation Server 2008.

    Open the TFSBuild.proj file from source control and merge the following lines in:

    <?xml version="1.0" encoding="utf-8"?>
    <Project DefaultTargets="DesktopBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5"> 
        <!-- Do not edit this --> 
        <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" /> 
        <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.targets" /> 
        <ProjectExtensions> 
            <!-- ... --> 
        </ProjectExtensions> 

        <!-- At the end of file: --> 

        <ItemGroup> 
            <AdditionalReferencePath Include="$(ProgramFiles)\Nunit 2.4.7\bin\" /> 
        </ItemGroup> 

        <Target Name="AfterCompile"> 
            <!-- Create a Custom Build Step --> 
            <BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Name="NUnitTestStep" Message="Running NUnit Tests"> 
                <Output TaskParameter="Id" PropertyName="NUnitStepId" /> 
            </BuildStep> 

            <!-- Get Assemblies to test --> 
            <ItemGroup> 
                <TestAssemblies Include="$(OutDir)\**\Calculator.dll"/> 
            </ItemGroup> 

            <!-- Run NUnit and check the result --> 
            <NUnit ContinueOnError="true" Assemblies="@(TestAssemblies)" OutputXmlFile="$(OutDir)nunit_results.xml" ToolPath="$(ProgramFiles)\Nunit 2.4.8\bin\"> 
                <Output TaskParameter="ExitCode" PropertyName="NUnitResult" /> 
            </NUnit> 
            <BuildStep Condition="'$(NUnitResult)'=='0'" TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Id="$(NUnitStepId)" Status="Succeeded" /> 
            <BuildStep Condition="'$(NUnitResult)'!='0'" TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Id="$(NUnitStepId)" Status="Failed" /> 

            <!-- Regardless of NUnit success/failure merge results into the build --> 
            <Exec Command="&quot;$(ProgramFiles)\nxslt-2.3-bin\nxslt2.exe&quot; &quot;$(OutDir)nunit_results.xml&quot; &quot;$(ProgramFiles)\MSBuild\NUnit\nunit transform.xslt&quot; -o &quot;$(OutDir)nunit_results.trx&quot;"/> 
            <Exec Command="&quot;$(ProgramFiles)\Microsoft Visual Studio 9.0\Common7\IDE\mstest.exe&quot; /publish:$(TeamFoundationServerUrl) /publishbuild:&quot;$(BuildNumber)&quot; /publishresultsfile:&quot;$(OutDir)nunit_results.trx&quot; /teamproject:&quot;$(TeamProject)&quot; /platform:&quot;%(ConfigurationToBuild.PlatformToBuild)&quot; /flavor:&quot;%(ConfigurationToBuild.FlavorToBuild)&quot;" IgnoreExitCode="true" /> 

            <!-- If NUnit failed it's time to error out --> 
            <Error Condition="'$(NUnitResult)'!='0'" Text="Unit Tests Failed" /> 
        </Target>
    </Project>

    4. Viewing test results

    When a build containing NUnit tests has succeeded, results of this tests are present in the build log:

    clip_image004

    When clicking the test results hyperlink, Visual Studio retrieves the result file from Team Foundation Server 2008 and displays it in the test results panel:

    clip_image006

    kick it on DotNetKicks.com


    Categories: C# | General | ICT | Internet | Testing | TFS | VSTS | NUnit

    Comments

    DotNetKicks.com |

    Monday, November 10, 2008 5:25 PM

    trackback

    Trackback from DotNetKicks.com

    Integrating NUnit test results in Team Build 2008

    alvinashcraft.com |

    Tuesday, November 11, 2008 2:42 PM

    pingback

    Pingback from alvinashcraft.com

    Dew Drop - November 11, 2008 | Alvin Ashcraft's Morning Dew

    Gavin Stevens United States |

    Monday, January 05, 2009 8:35 PM

    Gavin Stevens

    This is awesome!! Thanks man!

    Jon Kruger United States |

    Monday, January 19, 2009 2:51 AM

    Jon Kruger

    Thanks a ton for posting this.  I haven't tried it yet but this is going to save me a TON of time.  I really appreciate it, you've got me over the last hurdle that was preventing me from switching my project from MSTest to NUnit.

    Notes to Self |

    Thursday, February 19, 2009 1:29 AM

    trackback

    Trackback from Notes to Self

    Running NUnit tests as part of a TFS Build

    Comments are closed