17

I want to fail the build pipeline if a single test failed with azure pipelines.

Azure can successfully detect that my tests entered a failed state however it gives a success state to the entire build pipeline:

enter image description here

The question is how to make azure give a failed build state if the tests stage failed?

Here's my azure-pipelines.yml :

# Build ASP.NET Core project using Azure Pipelines
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core?view=vsts

pool:
  vmImage: 'Ubuntu 16.04'

variables:
  buildConfiguration: 'Release'

steps:
- script: |
    dotnet build --configuration $(buildConfiguration)
    dotnet test dotnetcore-tests --configuration $(buildConfiguration) --logger trx
    dotnet publish --configuration $(buildConfiguration) --output $BUILD_ARTIFACTSTAGINGDIRECTORY

- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'

- task: PublishBuildArtifacts@1
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Mawardy
  • 3,618
  • 2
  • 33
  • 37

3 Answers3

14

The original answer didn't work for me, but it looks like there was a lot of discussion on this, and there's now a failTaskOnFailedTests param for the task. That seems to work.


- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'
    failTaskOnFailedTests: true

I'm still surprised this wasn't default behavior.

Benjh
  • 5
  • 4
Jake Kreider
  • 950
  • 7
  • 12
5

Try to add failOnStandardError: 'true' in the task inputs:

- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'
    failOnStandardError: 'true'
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
1

Untick the below-highlighted option

enter image description here

tech-gayan
  • 1,373
  • 1
  • 10
  • 25
  • This is a good answer too because it allows you to do this at the pipeline level where you could have multiple pipelines with different behavior, using the code approach is good too but would then give you identical results in each pipeline where this doesn't. So it depends on your outcome, this is valid. – fr332lanc3 Feb 24 '21 at 16:07