RedRose
RedRose

Reputation: 679

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.

Upvotes: 2

Views: 1603

Answers (4)

Mathias Haugsbø
Mathias Haugsbø

Reputation: 428

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

Upvotes: 3

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35119

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

Upvotes: 0

Daniel Mann
Daniel Mann

Reputation: 58980

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.

Upvotes: 2

Zeeshan Abbas
Zeeshan Abbas

Reputation: 42

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'

Upvotes: -1

Related Questions