Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TFS 2017 - how to fail build when published Tests fail

I have a simple build process in TFS 2017 using CI/CD demo as described in https://msdn.microsoft.com/en-us/powershell/dsc/dsccicd

The Build definition contains four steps:

  • Run powershell script. As a part of the script, Pester tests are run on agent and results are saved to a folder using NUnit format
  • Publish Test results using from that folder
  • Copy files to staging directory
  • Publish Artifacts

When Pester test fails , I would like entire build to fail. At the moment build succeeds even when Published Test results show as failed ( In Issues section of Build Details). I don't see how can I force entire build to fail looking at the Build definition parameters.

like image 949
Sergei Avatar asked Sep 05 '25 03:09

Sergei


2 Answers

I'm not using TFS, but in my build process test failures trigger the build to fail by outputting an error.

This is done by adding the -PassThru switch to Invoke-Peter and sending the results of the command to a variable:

$TestResults = Invoke-Pester -Path .\Tests -PassThru

Then writing an error if there are any failed tests:

if($TestResults.FailedCount -gt 0)
{
    Write-Error "Failed '$($TestResults.FailedCount)' tests, build failed"
}

And then in the script after using Invoke-PSake:

exit ( [int]( -not $psake.build_success ) )
like image 68
Mark Wragg Avatar answered Sep 09 '25 19:09

Mark Wragg


You could use Logging Commands and exit code to fail a build task, then fail the entire build.

 Write-Error ("Some error")
 exit 1

Add a powershell task to catch the published Tests logs or status to judge if you have to fail the task. More details about how to fail a vNext build please refer this question: How to fail the build from a PowerShell task in TFS 2015

like image 31
PatrickLu-MSFT Avatar answered Sep 09 '25 20:09

PatrickLu-MSFT