Monday, February 9, 2009

Continuous Integration - Force a build to fail when unit tests fail and create work item



I had to setup continuous build on my TFS2008 machine which had to run a specific set of tests before it created a build (whenever anyone check’s in any code). I was pretty much successful doing that. The process was running smooth but whenever a test used to fail the build used to “Partially Fail” and also it didn’t create a “Bug” and assign it to the person who broke the build. A Bug was assigned to the person who broke the build only when the build used to “Fail”.
I did some RnD in this and figured out a way to force a build to fail whenever a test failed. I wrote the below line in the TFSBuild.proj file

< treattestfailureasbuildfailure > true < /treattestfailureasbuildfailure >
Adding this I thought, as I am forcing the test fail to be treated as a Build Failure, TFS will take care of raising a work item or Bug to the person but it didn’t.
Then I wrote a Target in the same file to create a work item on Test failure.



<UsingTask TaskName="Microsoft.TeamFounadtion.Build.Tasks.CreateNewWorkItem"

AssemblyFile="$(TeamBuildRefPath)\Microsoft.TeamFoundation.Build.Tasks.VersionControl.dll" />


<Target Name="AfterTest">

<!-- Refresh the build properties. -->

<GetBuildProperties TeamFoundationServerUrl="$(TeamFoundationServerUrl)"

BuildUri="$(BuildUri)"

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

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

</GetBuildProperties>

<!-- Set CompilationStatus to Failed if TestSuccess is false. -->

<SetBuildProperties TeamFoundationServerUrl="$(TeamFoundationServerUrl)"

BuildUri="$(BuildUri)"

CompilationStatus="Failed"

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

</SetBuildProperties>

<CreateNewWorkItem

BuildNumber="$(BuildNumber)"

BuildURi="$(BuildURI)"

Description="The CreateNewWorkItem task created this bug."

TeamProject="$(TeamProject)"

TeamFoundationServerUrl="$(TeamFoundationServerUrl)"

Title="Unit Test Failure in $(BuildNumber)"

WorkItemFieldValues="$(WorkItemFieldValues)"

WorkItemType="$(WorkItemType)"

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

</CreateNewWorkItem>

</Target>

No comments: