3
votes

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.

3

3 Answers

5
votes

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 ) )
2
votes

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

1
votes

VSTS and TFS 2017 set an environment variable for the current job status.

So set the Publish Test Results task as Always Run, then add the following PowerShell task to fail the build:

https://docs.microsoft.com/en-us/vsts/pipelines/build/variables?view=vsts&tabs=batch

    $hasTestFailed = [Environment]::GetEnvironmentVariable("agent.jobstatus")
    if ( $hasTestFailed -ne 'Succeeded')
    {
      exit 666
    }