在 Azure Devops 中发布之前访问 junit 文件 TestResults.xml

问题描述 投票:0回答:1

我在 ADO(yaml 管道)中有一个 powershell 任务来运行单元测试。
我还有一个单独的任务来发布上述任务之后的测试结果文件。

对于我正在处理的一些要求,我需要从

run unit tests powershell script task
访问 TestResults.xml 文件,该文件在
publish test results task
之前执行。

当我尝试从放置位置读取 xml 文件时,失败并提示该文件不存在。
有没有办法在发布之前访问 TestResults.xml 文件?

azure-devops azure-pipelines build-pipeline
1个回答
0
投票

我不确定你是如何生成

TestResults.xml
的。但根据您的描述,您似乎可以使用
publish test results task
成功发布测试结果。如果是这样,您可以在
TestResults.xml
调试日志 中找到
publish test results task
的路径,然后在 PowerShell 任务中访问它。

例如,

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

enter image description here

在我的示例中,我在 Maven 项目中运行 JUnit 测试,并使用 Surefire 插件生成测试结果文件。默认情况下,这些文件在

${basedir}/target/surefire-reports/TEST-*.xml
中生成。

在下面的yaml中,管道使用

Get-Content "target/surefire-reports/TEST-MyTest.xml"
访问测试结果文件并将其复制到
$(Build.ArtifactStagingDirectory)
,然后将其发布到构建工件。

- 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'
© www.soinside.com 2019 - 2024. All rights reserved.