getting a work item created on test failures

Nov 9, 2010 at 4:55 PM

I have found this project very useful in integrating nunit test into our team builds. One requirement we had though was to create a work item on test failures. I thought i would share this with everyone in case anyone else has this requirement. Below is the part of my modified tfsbuild.proj i used to achieve this.

I have changed the target where the tests are run to BeforeTest rather than AfterCompile so the assocaited changesets are added to the build log.

There is also a commented out step in the FailTheBuild target that can be used to mark the build as failed if any of the tests fail rather than marking it as partially succeeded

 

Anthony

<

Target Name="BeforeTest"

>

<!--

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)\**\WebService_Client_UnitTest.dll"

/>

</

ItemGroup

>

<!--

Run NUnit and check the result

-->

<

NUnit ContinueOnError="true" Assemblies="@(TestAssemblies)" OutputXmlFile="$(OutDir)nunit_results.xml" ToolPath="$(NUNitPath)"

>

<

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;$(MSBuildProjectDirectory)\NUnitTFS.exe&quot; -n &quot;$(OutDir)nunit_results.xml&quot; -t &quot;$(TeamProject)&quot; -b &quot;$(BuildNumber)&quot; -f &quot;%(ConfigurationToBuild.FlavorToBuild)&quot; -p &quot;%(ConfigurationToBuild.PlatformToBuild)&quot; -x &quot;$(MSBuildProjectDirectory)\NUnitToMSTest.xslt&quot;"

/>

<!--

If NUnit failed it's time to error out

-->

<

Error Condition="'$(NUnitResult)'!='0'" Text="Unit Tests Failed"

/>

 

<!--

go to the error target if there was an error

-->

<

OnError ExecuteTargets="FailTheBuild"

/>

 

</

Target

>

<

Target Name="FailTheBuild"

>

<!--

We are failing the build due to nunit failure issues

-->

<!--

<BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)"

BuildUri="$(BuildUri)"

Id="$(NUnitStepId)"

Status="Failed"

Message="Unit tests failed"/>

<GetBuildProperties TeamFoundationServerUrl="$(TeamFoundationServerUrl)"

BuildUri="$(BuildUri)"

Condition=" '$(IsDesktopBuild)' != 'true' ">

<Output TaskParameter="TestSuccess" PropertyName="TestSuccess" />

</GetBuildProperties>

<SetBuildProperties TeamFoundationServerUrl="$(TeamFoundationServerUrl)"

BuildUri="$(BuildUri)"

CompilationStatus="Failed"

Condition=" '$(IsDesktopBuild)' != 'true' and '$(TestSuccess)' != 'true' ">

</SetBuildProperties>

-->

<!--

end of part that fails the whold build on an nunit failure

-->

 

<!--

create a work item for a failed build

-->

<

CreateProperty Value="Work Item created by Team Build on Nunit failure. "

>

<

Output TaskParameter="Value" PropertyName="DescriptionText"

/>

</

CreateProperty

>

<

CreateProperty Value="

Build log is at

 

&lt;

a

href='file:///$(DropLocation)\$(BuildNumber)\BuildLog.txt'

 

&gt;

$(DropLocation)\$(BuildNumber)\BuildLog.txt

 

&lt;/a &gt;

.

 

"

>

<

Output TaskParameter="Value" PropertyName="BuildlogFile"

/>

</

CreateProperty

>

<

CreateProperty Value="

Test Results are at

 

&lt;

a

href='file:///$(DropLocation)\$(BuildNumber)\TestResults'

 

&gt;

$(DropLocation)\$(BuildNumber)\TestResults

 

&lt;/a &gt;

.

 

"

>

<

Output TaskParameter="Value"

 

PropertyName="TestResultsFolder"

 

Condition="Exists('$(DropLocation)\$(BuildNumber)\TestResults')"

/>

</

CreateProperty

>

<

BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Name="CreateWorkItemStep" Message="Creating work item"

>

<

Output TaskParameter="Id" PropertyName="CreateWorkItemStepID"

/>

</

BuildStep

>

<

CreateNewWorkItem

 

BuildId="$(BuildNumber)"

 

Description="Nunit tests have failed. &lt;p&gt;$(BuildlogFile)&lt;/p&gt; &lt;p&gt;$(TestResultsFolder)&lt;/p&gt;"

 

TeamProject="$(TeamProject)"

 

TeamFoundationServerUrl="$(TeamFoundationServerUrl)"

 

Title="Nunit failures in build $(BuildNumber)"

 

WorkItemFieldValues="$(WorkItemFieldValues)"

 

WorkItemType="$(WorkItemType)"

 

ContinueOnError="true"

/>

<

BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Id="$(CreateWorkItemStepID)" Status="Succeeded"

/>

 

</

Target

>

Coordinator
Nov 10, 2010 at 9:39 PM

Thanks for sharing that with everyone, though unfortunately it looks like your formatting got screwed up :-(