4

I want the PublishTestResults@2 task to run ONLY if the previous task script (which runs unit tests) actually ran.

  • If I use condition: succeededOrFailed() then PublishTestResults@2 runs even if the previous step did not run - I thought this was what condition: always() was for.
  1. How does one make a task contingent on the previous task even if the previous task failed?
  2. What is the difference between always() and succeededOrFailed()?

Reference

    # This step only runs if the previous step was successful - OK
    - script: |
        cd $(System.DefaultWorkingDirectory)/application/src
        yarn test:unit --silent --ci --reporters=jest-junit
      displayName: 'Jest Unit Tests'
      env:
        JEST_JUNIT_OUTPUT_DIR: $(System.DefaultWorkingDirectory)/testresults
        JEST_JUNIT_OUTPUT_NAME: 'jest-junit.xml'

    # This step ALWAYS runs - NO
    #  This step should ONLY run if the previous step RAN (success OR fail)
    - task: PublishTestResults@2
      displayName: 'Frontend Test results'
      condition: succeededOrFailed()
      inputs:
        testResultsFormat: JUnit
        searchFolder: $(System.DefaultWorkingDirectory)/testresults
        testResultsFiles: 'jest-junit.xml'
        testRunTitle: 'Frontend Test Results'
        mergeTestResults: false
        failTaskOnFailedTests: true

UPDATE: I suspect the Publish step "Frontend Test results" is running because the 2 previous steps were NOT run but the one before that was successful:

enter image description here

1 Answer 1

8

succeededOrFailed for a setp is equivalent to in(variables['Agent.JobStatus'], 'Succeeded', 'SucceededWithIssues', 'Failed') and this is why your Frontend Test results ran.

If you want to publish only when Jest Unit Tests was executed you may use logging commands to set variable and then use this variable in condition:

 # This step only runs if the previous step was successful - OK
    - script: |
        echo "##vso[task.setvariable variable=doThing;isOutput=true]Yes" #set variable doThing to Yes
        cd $(System.DefaultWorkingDirectory)/application/src
        yarn test:unit --silent --ci --reporters=jest-junit
      displayName: 'Jest Unit Tests'
      name: JestUnitTests
      env:
        JEST_JUNIT_OUTPUT_DIR: $(System.DefaultWorkingDirectory)/testresults
        JEST_JUNIT_OUTPUT_NAME: 'jest-junit.xml'

    # This step ALWAYS runs - NO
    #  This step should ONLY run if the previous step RAN (success OR fail)
    - task: PublishTestResults@2
      displayName: 'Frontend Test results'
      condition: and(succeededOrFailed(), eq(variables['JestUnitTests.doThing'], 'Yes'))
      inputs:
        testResultsFormat: JUnit
        searchFolder: $(System.DefaultWorkingDirectory)/testresults
        testResultsFiles: 'jest-junit.xml'
        testRunTitle: 'Frontend Test Results'
        mergeTestResults: false
        failTaskOnFailedTests: true
Sign up to request clarification or add additional context in comments.

2 Comments

OK, so is there no difference between always() and succeededOrFailed()?
@Marc, always() will run even if the run is cancelled while succeededOrFailed() will run either the pipeline failed or not, but if it is cancelled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.