Question: 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](https://i.stack.imgur.com/mqEAo.png)](https://i.stack.imgur.com/mqEAo.png)

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

```

Answer: 
The original answer didn't work for me, but it looks like there was [a lot of discussion](https://github.com/Microsoft/azure-pipelines-tasks/issues/1268) on this, and there's now a [`failTaskOnFailedTests`](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/test/publish-test-results?view=azure-devops&tabs=yaml#yaml-snippet) 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.
Try to add `failOnStandardError: 'true'` in the task inputs:

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

```