This is actually rather complicated to directly detect within MSBuild - the TestToolsTask (which is responsible for actually running your tests) has ContinueOnError true, which converts all errors into warnings such that the build can continue even when tests fail. This means that you cannot distinguish test success / failure by the Targets which get executed. There are no properties which get set on test failure within MSBuild either, so there's nothing to include in conditions, etc.
You can, howevber, distinguish the case you are interested in in code by writing a custom task. There is a property in the BuildData class returned from the GetBuildDetails web method called GoodBuild that is set to true when compilation and tests succeed and false otherwise. This value is set in the AfterTest target, so if you override a target that occurs after this one, you should be fine. I would suggest AfterEndToEndIteration - this will already weed out cases where compilation has failed, since this target only gets executed when compilation is successful. You would want something like this in TfsBuild.proj:
< UsingTask TaskName="MyNamespace.HandleCompilationAndTestSuccess" AssemblyFile="MyAssembly.dll"/>
< Target Name="AfterEndToEndIteration">
< HandleCompilationAndTestSuccess TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" />
</ Target>
Then, in your HandleCompilationAndTestSuccess task's Execute method, you can check the value of GoodBuild with some code like this:
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(TeamFoundationServerUrl);
BuildStore buildStore = (BuildStore)tfs.GetService( typeof(BuildStore));
if (buildStore.GetBuildDetails(BuildUri).GoodBuild)
{
// Do custom logic here to handle compilation and test success.
}
Note that this assumes you have defined properties in your task named TeamFoundationServerUrl and BuildUri. Hope this helps!
-Aaron
http://blogs.msdn.com/aaronhallberg |