It's all about the answers!

Ask a question

JazzBuildEngine understands MSBuild and NUnit for .NET dev.


Gerald Pape (8123) | asked Jul 24 '09, 5:36 a.m.
To use MSBuild to build .NET projects and test them with nUnit you need the following:

Preparation Step:
Download to Build Machine:
    ANT .NET library from http://ant.apache.org/antlibs/dotnet/
    .NET Framework from http://www.microsoft.com/net/Download.aspx
    NUnit from http://nunit.org/index.php?p=download

Installation on Build Machine:
    Install ANT .NET library into ant/lib
    Install .NET Framework
    Install NUnit Package


You will also need to provide other required tools and any dependencies for your build, Set environment variables and so on.

Setup your build engine, build user, build respository workspace as described on jazz.net in artikel
Getting started with Setting up jazz builds.

Step 1 : Add the definition for the ANT .NET Library:
<project default="all" name="HelloWorld" xmlns:dn="antlib:org.apache.ant.dotnet">

Step 2 : Call MSBuild with the appropriate Visual Studio Solution file (using the default settings).
<dn:msbuild buildfile="Helloworld.net.sln" />

Step 3 : Execute NUnit to run unit tests
However, you could also use the NUnit Task provided by the dotNet Antlibs. NUnit creates the test output like JUnit but in a different format
<exec executable="${nunit.path}${nunit.exec}">
<arg value="${nunit.testfile}"/>
<arg value="/xml=${nunit.xmlfile}"/>
</exec>

Step 4 : Transform the NUnit output file a JUnit compatible format, and tell the JUnit logpublisher where to get the outputfile.
<xslt in="${nunit.xmlfile}" out="${nunit.transfomedfile}" style="${nunit.transformation}" />
<junitLogPublisher filePath="${nunit.transfomedfile}" buildResultUUID="${buildResultUUID}" repositoryAddress="${repositoryAddress}" userId="${userId}" password="${password}" />

Now running a build also shows you the NUnit results (as JUnit)

Sample build file:
<?xml version="1.0" encoding="UTF-8"?>


<project default="all" name="HelloWorld" xmlns:dn="antlib:org.apache.ant.dotnet">


<target name="all">
<!-- userid and password of the build engine -->
<property name="userId" value="userid" />
<property name="password" value="yourtopsecretpwd" />

<property name="compileLog" value="${basedir}/compile.xml" />

<startBuildActivity activityIdProperty="Build" label="building..." buildResultUUID="${buildResultUUID}" repositoryAddress="${repositoryAddress}" userId="${userId}" password="${password}" />
<dn:msbuild buildfile="Helloworld.net.sln" />
<completeBuildActivity activityId="${Build}" buildResultUUID="${buildResultUUID}" repositoryAddress="${repositoryAddress}" userId="${userId}" password="${password}" />

<startBuildActivity activityIdProperty="Test" label="testing..." buildResultUUID="${buildResultUUID}" repositoryAddress="${repositoryAddress}" userId="${userId}" password="${password}" />
<property name="nunit.path" value="C:\Program Files\NUnit 2.5.1\bin\net-2.0\"/>
<property name="nunit.exec" value="nunit-console.exe"/>
<property name="nunit.testfile" value="D:\BuildEngine\RTC pilot\Workspace\net-fetched\HelloWorld.Net\HelloworldTest\bin\Debug\HelloworldTest.exe"/>
<property name="nunit.xmlfile" value="D:\BuildEngine\RTC pilot\Workspace\net-fetched\HelloWorld.Net\nunit.xml"/>

<exec executable="${nunit.path}${nunit.exec}">
<arg value="${nunit.testfile}"/>
<arg value="/xml=${nunit.xmlfile}"/>
</exec>
<junitLogPublisher filePath="${nunit.xmlfile}" buildResultUUID="${buildResultUUID}" repositoryAddress="${repositoryAddress}" userId="${userId}" password="${password}" />
<completeBuildActivity activityId="${Test}" buildResultUUID="${buildResultUUID}" repositoryAddress="${repositoryAddress}" userId="${userId}" password="${password}" />

</target>
<taskdef name="junitLogPublisher" classname="com.ibm.team.build.ant.task.JUnitLogPublisherTask" />
<taskdef name="logPublisher" classname="com.ibm.team.build.ant.task.LogPublisherTask" />
<taskdef name="startBuildActivity" classname="com.ibm.team.build.ant.task.StartBuildActivityTask" />
<taskdef name="completeBuildActivity" classname="com.ibm.team.build.ant.task.CompleteBuildActivityTask" />

</project>

Sample XSLT to transform NUnit to JUnit:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8" version="1.0" cdata-section-elements="failure" />
<xsl:template match="test-results/test-suite">
<xsl:variable name="nErrors" select="//test-results/@errors"/>
<xsl:variable name="nFailures" select="//test-results/@failures"/>
<xsl:variable name="nTotal" select="//test-results/@total"/>
<xsl:variable name ="nHostName" select="//test-results/environment/@machine-name"/>
<xsl:variable name="testcases" select="//test-suite"/>
<testsuite name="{//test-suite/@name}" time="{@time}" tests="{$nTotal}" errors="{$nErrors}" failures="{$nFailures}" hostname="{$nHostName}">
<xsl:for-each select="$testcases">
<xsl:variable name="suite" select="."/>
<xsl:variable name="generalfailure" select="./failure"/>
<xsl:for-each select=".//test-case">
<testcase classname="{./@name}" name="{./@name}" time="{./@time}">
<xsl:if test="./failure">
<xsl:variable name="failstack" select="count(./failure/stack-trace/*) + count(./failure/stack-trace/text())"/>
<failure type="nunit.not.reported">
<xsl:choose>
<xsl:when test="$failstack > 0 or not($generalfailure)">
MESSAGE: <xsl:value-of select="./failure/message"/>
+++++++++++++++++++
STACK TRACE: <xsl:value-of select="./failure/stack-trace"/>
</xsl:when>
<xsl:otherwise>
MESSAGE: <xsl:value-of select="$generalfailure/message"/>
+++++++++++++++++++
STACK TRACE: <xsl:value-of select="$generalfailure/stack-trace"/>
</xsl:otherwise>
</xsl:choose>
</failure>
</xsl:if>
</testcase>
</xsl:for-each>
</xsl:for-each>
</testsuite>
</xsl:template>
</xsl:stylesheet>

http://web3.twitpic.com/img/19199240-54d4fabe7f933378d36a6fab6e732f40.4a697f61-full.jpg

5 answers



permanent link
Ralph Schoon (63.2k33646) | answered Jul 27 '09, 2:16 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Thanks for this great post! This will be very usefull! :D

Ralph

permanent link
Gary Mullen-Schultz (28725536) | answered Sep 04 '09, 11:24 a.m.
This is very helpful.

Does anyone know how to pass properties on to MSBuild? I'm trying to change the output directory with no luck. I've tried various things such as:

<dn>
<property>
</dn>

but have not had any luck. I have tried several variants of the "OutputDirectory" name as well.

Thanks, Gary

permanent link
Nick Edgar (6.5k711) | answered Sep 11 '09, 11:35 a.m.
JAZZ DEVELOPER
Nice example, thanks. The sample build script doesn't show applying the XSLT transform, though -- it attempts to pass the NUnit XML file to junitLogPublisher directly, but it's easy to sort this out given the prior snippets.

A similar approach could be used to publish compilation logs for other languages via jdtCompileLogPublisher. Do you know whether the various .NET compilers can output in a structured format (i.e. XML)?

We're looking to provide more direct support for other unit testing frameworks and compilers in 3.0, and deliver these enhancements as separate plug-ins / jars so they can also work against 2.0. e.g. see story 94286: Improve integration with other unit testing frameworks and its child task.

Nick Edgar
RTC Build component lead

permanent link
Nick Edgar (6.5k711) | answered Sep 11 '09, 12:07 p.m.
JAZZ DEVELOPER

permanent link
Nick Edgar (6.5k711) | answered Nov 26 '09, 1:57 p.m.
JAZZ DEVELOPER
RTC 2.0.0.2 now includes support for publishing CPPUnit, NUnit, and MSTest results via dedicated Ant tasks. These are also available in 3.0 M2.
For more details, see
94286: Improve integration with other unit testing frameworks and its child items.

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.