【问题标题】:Publish code coverage not finding coverage file in Azure DevOps发布代码覆盖率未在 Azure DevOps 中找到覆盖率文件
【发布时间】:2021-08-13 08:21:59
【问题描述】:

我正在使用 Node 14.x 和 Jest 26.x。 package.json 文件中有一个npm test 脚本,其中包含以下内容:

cross-env NODE_ENV=test jest --coverage --forceExit

当我在本地运行它时,它会在./coverage 目录中生成代码覆盖率报告。 ./coverage目录内容如下:

lcov-report (folder)
clover.xml
coverage-final.json
lcov.info

看起来clover.xml 包含代码覆盖率报告。在lcov-report 文件夹中可以找到更多详细信息。 我为代码覆盖率设置了 Azure DevOps 管道,如下所示:

...
- task: PublishTestResults@2
  condition: succeededOrFailed()
  inputs:
    testRunner: JUnit
    testResultsFiles: '**/junit.xml'

# Publish code coverage results
# Publish Cobertura or JaCoCo code coverage results from a build
- task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/coverage/clover.xml'

运行管道后,Azure DevOps 似乎找不到 cover.xml 文件。我收到以下错误:

##[debug]Result: true
##[debug]Report directory: /home/vsts/work/_temp/cchtml
Reading code coverage summary from '/home/vsts/work/1/s/coverage/clover.xml'
##[warning]No coverage data found. Check the build errors/warnings for more details.
##[debug]Processed: ##vso[codecoverage.publish codecoveragetool=Cobertura;summaryfile=/home/vsts/work/1/s/coverage/clover.xml;reportdirectory=/home/vsts/work/_temp/cchtml;]

我还为summaryFileLocation 尝试了以下选项,但都导致相同的错误。

'**/coverage/clover.xml'
'$(System.DefaultWorkingDirectory)/**/coverage/clover.xml'

我了解clover.xml 的格式可能与Cobertura 或JaCoCo 的格式不同,但至少Azure 应该能够找到该文件。 我错过了什么?

【问题讨论】:

    标签: unit-testing azure-devops jestjs code-coverage


    【解决方案1】:

    我偶然发现了一条路。

    看起来Jest 可以生成cobertula 覆盖率报告。它需要在 Jest 配置中添加。我在jest.config.js 中添加了以下内容:

    coverageReporters: ['text', 'text-summary', 'clover', 'cobertura']
    

    Jest 在以下位置生成了cobertura 覆盖文件:

    ./coverage/cobertura-coverage.xml
    

    接下来我修改了 Azure 管道文件如下:

    - task: PublishCodeCoverageResults@1
      inputs:
        codeCoverageTool: Cobertura
        summaryFileLocation: '$(System.DefaultWorkingDirectory)/coverage/cobertura-coverage.xml'
    

    更改后,当我运行管道时,Azure DevOps 可以找到文件并显示覆盖率报告!

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2021-05-25
      • 2021-02-11
      • 2017-03-17
      • 2020-07-08
      • 2020-05-22
      • 2021-01-17
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多