Send Ms Teams notification when tests fails in Azure DevOps pipeline

Viewed 467

I have e2e tests which will be triggered to run at midnight. Now I want to send notification to a MS Teams channels when the test fails.

- task: O365PostMessageRelease@0
   inputs:
      addressType: 'url'
      url: 'https://hans.webhook.office.com/webhookb2/cfe11f3f-69ee-4447-ad1b'
      messageType: 'message'
      title: 'Test Result'
      summary: 'test result summary'
      text: 'here you can see the last test run results'
      includeLink: true
      linkText: 'View Release Detail'

the test results are puplished as an artifact like this

      - publish: junit-result.xml
        artifact: cypress_results_combined

Now I want to send the notification only when the test fails. How could I achieve that?

2 Answers

You can specify a condition to the O365PostMessageRelease@0 so that it runs only when that condition is met.

You can specify the conditions under which each stage, job, or step runs. By default, a job or stage runs if it does not depend on any other job or stage, or if all of the jobs or stages that it depends on have completed and succeeded. By default, a step runs if nothing in its job has failed yet and the step immediately preceding it has finished. You can customize this behavior by forcing a stage, job, or step to run even if a previous dependency fails or by specifying a custom condition.

You can specify conditions under which a step, job, or stage will run.

  • Only when all previous dependencies with the same agent pool have succeeded. If you have different agent pools, those stages or jobs will run concurrently. This is the default if there is not a condition set in the YAML.

  • Even if a previous dependency has failed, unless the run was canceled. Use succeededOrFailed() in the YAML for this condition.

  • Even if a previous dependency has failed, even if the run was canceled. Use always() in the YAML for this condition.

  • Only when a previous dependency has failed. Use failed() in the YAML for this condition.

  • Custom conditions

Depending on how test execution is carried out and how a failure is handled (this part is left out of the question) there is a few options

  1. Run the notification task only when the pipeline has failed by specifying condition: failed().
  2. Create a custom condition based on the output of the test execution step/job

There are more detials and examples in the section about Specifying conditions in the documentation

If your test step doesn't return error code to pipeline(show passed), but only record test result in junit-result.xml file, you need to parse the xml file(eg: powershell task) and get the 'failed' test case value, set it as variable, evaluate the variable value in condition for the notification task.

Related