Search code examples
azure-devopsazure-pipelinesbuild-pipeline

Access junit file TestResults.xml before publishing it in Azure Devops


I have a powershell task in ADO (yaml pipeline) to run unit tests.
I also have a separate task to publish the test results file which follows the above task.

For some requirements that I'm working on, I need to access the TestResults.xml file from the run unit tests powershell script task which is executed before the publish test results task.

When I tried reading the xml file from the drop location, it failed saying that the file does not exist.
Is there a way to access the TestResults.xml file before it is published?


Solution

  • I'm not sure how you generate TestResults.xml. But according to your description, it seems that you can successfully publish test results using the publish test results task. If so, you can find the path of your TestResults.xml in the debug log of publish test results task and then access it in your PowerShell task.

    For example, ##[debug]Reading test results from file 'D:\a\1\s\target\surefire-reports\TEST-MyTest.xml'.

    enter image description here

    In my example, I am running JUnit tests in a maven project and using Surefire Plugin to generate test result files. By default, these files are generated in ${basedir}/target/surefire-reports/TEST-*.xml.

    In the yaml below, the pipeline accessed the test result file using Get-Content "target/surefire-reports/TEST-MyTest.xml" and copied it to $(Build.ArtifactStagingDirectory), and then publish it to build artifact.

    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          mvn test -f pom.xml 
          Get-Content "target/surefire-reports/TEST-MyTest.xml"
          Copy-Item -Path "target/surefire-reports/TEST-MyTest.xml" -Destination $(Build.ArtifactStagingDirectory)
        workingDirectory: '$(Build.SourcesDirectory)'
    - task: PublishTestResults@2
      inputs:
        testResultsFormat: 'JUnit'
        testResultsFiles: '**/TEST-*.xml'
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'drop'
        publishLocation: 'Container'