4

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.

Sergei
  • 387
  • 1
  • 6
  • 16

3 Answers3

5

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 ) )
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • Thank you Mark, I am already doing something like this. If Pester tests fail, script returns error to TFS: ' Write-Output "##vso[task.logissue type=error]$errorMessage" . This does not however fail the entire build. – Sergei Jun 30 '17 at 09:48
  • `Write-Output` is not triggering an exception, it's just sending text to the default output stream (the host). Change `Write-Output` to `Write-Error` and see if that helps (it should trigger an exception while also sending text to the hosts error stream). – Mark Wragg Jun 30 '17 at 09:51
  • I have replaced Write-Output by Write-error in line 21 https://github.com/PowerShell/Demo_CI/blob/master/InfraDNS/Build.ps1 , unfortunately this does not help . While UnitTest fails and shows as failed in Build status , the entire Build still happily completes – Sergei Jun 30 '17 at 10:18
  • 1
    In my `build.ps1` I also do this after `Invoke-psake` : `exit ( [int]( -not $psake.build_success ) )` – Mark Wragg Jun 30 '17 at 10:27
2

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

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
2

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://learn.microsoft.com/en-us/vsts/pipelines/build/variables?view=vsts&tabs=batch

    $hasTestFailed = [Environment]::GetEnvironmentVariable("agent.jobstatus")
    if ( $hasTestFailed -ne 'Succeeded')
    {
      exit 666
    }
DalSoft
  • 10,673
  • 3
  • 42
  • 55