Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fail the build pipeline if "Tests Failed" in azure pipelines?

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://docs.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
like image 592
Mawardy Avatar asked Oct 13 '18 04:10

Mawardy


People also ask

What are the two ways for Azure Pipelines to be built?

Azure Pipelines combines continuous integration (CI) and continuous delivery (CD) to test and build your code and ship it to any target. Continuous Integration (CI) is the practice used by development teams of automating merging and testing code.

How are test cases run in pipeline?

Select the test(s) you want to run, open the Run menu, and choose Run test. The test binaries for these tests must be available in the build artifacts generated by your build pipeline.

How do you trigger release pipeline from build pipeline Azure DevOps?

Stages filters for pipeline resource triggers requires Azure DevOps Server 2020 Update 1 or greater. You can trigger your pipeline when one or more stages of the triggering pipeline complete by using the stages filter. If you provide multiple stages, the triggered pipeline runs when all of the listed stages complete.

What happens when pipeline is paused?

However, if you pause a running Stream pipeline job, the current state of the job is saved and the job is gracefully stopped at that point. When the resume command is issued, a new job is started to restart the Pipeline Version from the previously saved state.


3 Answers

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.

like image 127
Jake Kreider Avatar answered Oct 02 '22 22:10

Jake Kreider


Untick the below-highlighted option

enter image description here

like image 44
tech-gayan Avatar answered Oct 02 '22 23:10

tech-gayan


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

- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'
    failOnStandardError: 'true'
like image 34
Shayki Abramczyk Avatar answered Oct 02 '22 23:10

Shayki Abramczyk