Thursday, January 24, 2013

How to Display Unit Test Report in Teamcity

You can configure NAnt/MsBuild  script to run unit test. You can understand how many tests passed or failed when you watch the build log. But the report is not sufficient to understand. You can see more detail report of Unit Test in Teamcity. What you have to do is to import the test result in teamcity. There have special messaging option of teamcity which is called Service Massages ( Service Message is used to pass commands/build information to Teamcity from the build script. CruiseControl.NET has same feature to integrate xml result to display graphical report.
Syntax of massaging is
   1: ##teamcity[message name1='value1' name2='value2']

In NAnt/Msbuild script when you run unit test using nunit-console.exe there have an option to get output result as *.xml format. NAnt script as

   1: <target name="NUnitTest" description="RunNunit Test">

   2:   <exec program="C:\nunit\bin\nunit-console.exe"

   3:       workingdir="D:\MyProject\Tests"

   4:       commandline="Business.Tests.dll /xml:TestResults.xml"/>

   5: </target>

For MsBuild

   1: <Target Name="Test">

   2:       <NUnit ToolPath="$(NUnitExePath)" Assemblies="Business.Tests.dll" OutputXmlFile="TestResults.xml" />

   3: </Target>

You will need for that.

This xml contains detail about which test method is failed, reason for fail and other meta information. Now I like to import that result in Teamcity so that we can use  teamcity feature to display result in teamcity.

For that we will use teamcity service messages options.

For NAnt

   1: <echo message="##teamcity[importData type='nunit' path='TestResults.xml']" /> 

For MsBuild

   1: <Message 

   2:          Text="##teamcity[importData type='nunit' 

   3:          Path='TestResults.xml']"

   4:          />

But this result does not show automatically in teamcity. There have a plugin to process this xml and display results in teamcity which is called Xml Report Processing. This plugin is integrated after version 4.5.

You can include plugin in your build configuration from MsBuild Configuration  adding Build Features


You can see the Xml Report processing result  in teamcity demo but this for old teamcity version 4.5. But now in Teamcity 7.x result is updated and integrated much features like time calculation, graphical representation. you can also investigate or mute failed tests.

There also have option to integrate Code coverage report in same way using teamcity service messaging