'How to fail AzureDevops pipeline when test files are not found
I have the following DotNet Test task in my pipeline
displayName: 'unit tests'
inputs:
command: test
projects: '**/*Unit*.csproj'
publishTestResults: true
arguments: '/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=results/'
How can I fail the pipeline if no files matched the project pattern: '**/*Unit*.csproj'?
Currently, it displays the current error message and moves on to the next task
##[warning]Project file(s) matching the specified pattern were not found.
Solution 1:[1]
Use the Visual Studio Test task. It has a minimumExpectedTests parameter, so if you set it to 1, the task will fail if 0 tests are run.
Solution 2:[2]
One could also run a bash script checking for the existence of the test result file (and that number of results is greater than 0):
- bash: |
if [ $(test_results_host_folder)**/*.trx ] && [ $(grep -E "<UnitTestResult" $(test_results_host_folder)**/*.trx -c) -gt 0 ]; then
echo "##vso[task.setVariable variable=TESTRESULTFOUND]true"
fi
displayName: Check if test results file & results >= 1
- script: |
echo No test result found
exit 1
displayName: No test result found
condition: ne(variables.TESTRESULTFOUND, 'true')
Solution 3:[3]
If you have stages in your yaml pipeline, then you can do something like this, this will not run the next stage if the previous stage has failed
stages:
- stage: unittests
displayName: 'unit tests'
- stage: nextstage
dependsOn: unittests
displayName: 'unit tests'
Solution 4:[4]
As far as I know, we cannot set the task itself to make the pipeline fail when it cannot find the file.
For a workaround:
You could use the Build Quality Checks task from Build Quality Checks extension.
This task can scan all set tasks and check warnings. If the number of warnings is greater than the set upper limit, the pipeline will fail.
Result:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daniel Mann |
| Solution 2 | Mathias Haugsbø |
| Solution 3 | Zeeshan Abbas |
| Solution 4 | Kevin Lu-MSFT |


