Tuesday, August 12, 2008

How to fail the build if unit tests fail.

I just realized that the way TFS is architected is that it will mark the tests "Partially Succeeded" if the unit tests fail. This does not agree with out development process. Our development process prescribes that the build should fail if any of the unit tests fail. One way to deal with this problem is to edit C:\Program Files\MSBuild\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets and set ContinueOnError to false.

Another way of dealing with this was suggested in the blog entry http://blogs.msdn.com/aaronhallberg/archive/2007/11/05/how-to-fail-a-build-when-tests-fail.aspx

I modified the code suggested there to make sure also that the files are not copied to the drop location if the unit tests fail.


<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 TeamFoundationServerUrl="$(TeamFoundationServerUrl)"


BuildUri="$(BuildUri)"


SkipDropBuild="true"


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


</Target>