Implementing Tests in a Pipeline
This lesson shows how to configure test tasks and agents, and how to integrate results.
Learning outcomes
After completing this lesson, you will be able to:
- Configure test and publishing tasks.
- Explain why publishing needs a condition.
- Configure service containers for tests.
- Report results in GitHub Actions.
Configuring test tasks in Azure Pipelines
Tests can run through a language command in a script step, or through a dedicated task. The following job runs .NET tests and publishes the results. The result file format must match the testResultsFormat input.
steps:
- task: UseDotNet@2
inputs:
version: 9.x
- script: dotnet test --configuration Release --logger trx --results-directory $(Agent.TempDirectory)/results
displayName: Run unit tests
- task: PublishTestResults@2
condition: succeededOrFailed()
inputs:
testResultsFormat: VSTest
testResultsFiles: '**/*.trx'
searchFolder: $(Agent.TempDirectory)/results
testRunTitle: Unit tests
failTaskOnFailedTests: falseThe condition: succeededOrFailed() line matters a great deal. By default, a step runs only if earlier steps succeeded, so without this condition, failing tests would stop the job before their results were published, and the Tests tab would show nothing at precisely the time that it is needed.
PublishTestResults@2 accepts JUnit, NUnit, VSTest, XUnit and CTest formats, with JUnit as the default. mergeTestResults combines files into one run, and more than 100 files are always merged. failTaskOnFailedTests makes the task fail if the results contain failures, which suits test tools that always exit successfully.
The VSTest task
VSTest@3 runs tests through the Visual Studio test platform, and it offers features that a plain command lacks. It can distribute tests across several agents with distributionBatchType, select tests with a filter, rerun failed tests, and run only tests affected by a change, a feature known as Test Impact Analysis. Tests can also be sliced across agents in a job with a parallel strategy.
jobs:
- job: test
strategy:
parallel: 4
steps:
- task: VSTest@3
inputs:
testSelector: testAssemblies
testAssemblyVer2: '**/*Tests.dll'
distributionBatchType: basedOnExecutionTimeThe following diagram shows the publishing task with the corrected condition.

PublishTestResults@2 and VSTest@3
Configuring test agents
Tests fail for reasons unrelated to the code when the agent lacks something. A test agent must have the right software, network access and permissions.
Software and browsers
Microsoft-hosted images include many browsers and test tools, and a setup task can add specific versions. Browser tests on a self-hosted agent need the browser and its driver installed, and user interface tests on Windows may need an interactive session, which is configured when the agent is installed.
Services and dependencies
Service containers provide dependencies such as a database for the duration of a job, on both platforms. This gives each run a clean dependency, without any shared test database that can hold stale data.
# Azure Pipelines
resources:
containers:
- container: db
image: postgres:17
env:
POSTGRES_PASSWORD: $(testDbPassword)
jobs:
- job: integration
pool:
vmImage: ubuntu-latest
services:
postgres: db
steps:
- script: npm run test:integration
# GitHub Actions
jobs:
integration:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:17
env:
POSTGRES_PASSWORD: ${{ secrets.TEST_DB_PASSWORD }}
ports:
- 5432:5432Tests that reach private systems need a self-hosted agent or a pool with virtual network access. Tests should not rely on the order in which they run, since parallel runs will expose hidden dependencies between them.
Tests that fail because of the agent
Integrating results in GitHub Actions
GitHub Actions has no built-in equivalent of the Tests tab. Teams use one of three approaches: upload the result file as an artifact, write a summary to $GITHUB_STEP_SUMMARY, or use a reporting action that creates annotations through the Checks API. The if: ${{ !cancelled() }} condition performs the same role as succeededOrFailed().
- name: Run tests
run: npx jest --ci --reporters=default --reporters=jest-junit
- name: Save the test report
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: test-results
path: junit.xmlA matrix strategy runs the same tests across versions or operating systems, as the module on orchestration explains. Results for each combination then need unique artifact names.
Test results in GitHub Actions
Missing test results
A pipeline runs tests and then publishes results with PublishTestResults@2. When tests pass, the Tests tab shows them. When a test fails, the tab is empty. What should you change?
Add condition: succeededOrFailed(), or always(), to the publishing task. The test step fails the job, so a later step is skipped under the default condition. Changing testResultsFormat would not help, because results appear correctly on a successful run. Setting continueOnError: true on the test step would allow publishing, but it would mark the failure as a warning, which is less suitable unless failTaskOnFailedTests is used too.
Which format value should PublishTestResults@2 use for .trx files?
Show answer
VSTest. JUnit is the default, so it must be set explicitly.
Exam Tips
Remember for the exam
- Use succeededOrFailed() on the publish task so that failed results appear.
- PublishTestResults@2 supports JUnit, NUnit, VSTest, XUnit and CTest.
- VSTest@3 adds distributed runs, reruns and Test Impact Analysis.
- Service containers give each job a clean dependency.
- In GitHub Actions, use if: ${{ !cancelled() }} or always() for reporting.