TFS 2017 - how to fail build when published Tests fail

谁都会走 提交于 2020-01-14 14:00:10

问题


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.


回答1:


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:


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




回答3:


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
    }


来源:https://stackoverflow.com/questions/44842693/tfs-2017-how-to-fail-build-when-published-tests-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!