How to fail AzureDevops pipeline when test files are not found

1.4k views Asked by At

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.

4

There are 4 answers

2
Zeeshan Abbas On

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'
1
Daniel Mann On

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.

0
Kevin Lu-MSFT On

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.

enter image description here

Result:

enter image description here

1
Mathias Haugsbø On

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')