Implementing tests in a pipeline
Implement tests in a pipeline
Publish result files when the runner does not
A raw script or runner can leave supported test-result files without sending them to Azure Pipelines. In that case, a separate PublishTestResults@2 step makes the results visible in the Tests tab and available to Test Analytics. Built-in tasks such as Visual Studio Test and .NET Core CLI can publish automatically, and several other tasks expose integrated publication, so they do not need a duplicate publish step.
- task: PublishTestResults@2
inputs:
testResultsFormat: 'JUnit' # JUnit | NUnit | VSTest | xUnit | CTest
testResultsFiles: '**/TEST-*.xml'
mergeTestResults: true
condition: succeededOrFailed()Two details that matter:
condition: succeededOrFailed()— use it on a separate publisher that must run after an earlier failure in a non-cancelled run. The default step behavior runs only while nothing in the job has failed. Usealways()if the publisher must also run after cancellation.- Multiple formats are supported: JUnit, NUnit, VSTest, xUnit, CTest. The format must match what the test runner emits.
mergeTestResults: truecombines files into one run. More than 100 result files are merged even when this input isfalse.
Test agents and parallelism
Distributing tests across agents can cut wall-clock time. Each agent runs one job at a time, so parallel testing requires multiple agents, sufficient parallel-job capacity, strategy: parallel, and slices that can run independently. Use the supplied slice-position variables so failures can be reproduced against the same partition.
Results as a gate
Publication and enforcement are separate. Published results feed the Tests tab and Test Analytics. The test command can fail the job itself, or a standalone publisher can use failTaskOnFailedTests: true because its default is false. A branch build-validation policy then evaluates the pipeline's build result to decide whether the pull request can complete; it does not directly query Test Analytics.
Primary sources
- https://learn.microsoft.com/en-us/credentials/certifications/resources/study-guides/az-400
- https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/publish-test-results-v2
- https://learn.microsoft.com/en-us/azure/devops/pipelines/process/conditions
- https://learn.microsoft.com/en-us/azure/devops/pipelines/test/review-continuous-test-results-after-build
- https://learn.microsoft.com/en-us/azure/devops/pipelines/test/parallel-testing-any-test-runner
- https://learn.microsoft.com/en-us/azure/devops/pipelines/test/test-analytics
- https://learn.microsoft.com/en-us/azure/devops/pipelines/test/review-code-coverage-results
- https://learn.microsoft.com/en-us/azure/devops/repos/git/branch-policies