运行 Azure DevOps 上的 Jasmine 测试作为自动构建过程的一部分

Running Jasmine tests on Azure DevOps as part of automated build process

鉴于构建中有一个 Angular 应用程序,因此其中有 Jasmine 测试。我需要做什么才能将这些测试结果作为构建的一部分发布,并且更好的是,在成功执行所有 Jasmine 测试时关闭构建结果?

您可以通过以下脚本和任务执行此操作:

  1. 运行 ng test
  2. 使用 PublishTestResults 任务发布测试结果
  3. 使用 PublishCodeCoverageResults 任务发布代码覆盖率结果

Azure Pipelines YAML 文件中,这可能如下所示:

# perform unit-tests and publish test and code coverage results
- script: |
    npx ng test --watch=false --karmaConfig karma.conf.ci.js --code-coverage
  displayName: 'perform unit tests'    

- task: PublishTestResults@2
  condition: succeededOrFailed()
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: '**/TESTS-*.xml'
  displayName: 'publish unit test results'

- task: PublishCodeCoverageResults@1
  displayName: 'publish code coverage report'
  condition: succeededOrFailed()
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(Build.SourcesDirectory)/coverage/cobertura-coverage.xml'
    failIfCoverageEmpty: true     

@uminder 的 Azure 配置是正确的。

我要添加两件事,这样答案就完整了。这是创建 junit 报告和覆盖率文件所必需的 - 因此您稍后可以在 azure 管道中引用它们。

  1. junit 和报道(如果不存在)报告给 karma.config.js
 config.set({
      plugins: [
        ...
        require('karma-coverage'),
        require('karma-junit-reporter')
      ]

当然要安装

npm install -d karma-junit-reporter

  1. 我还会在 coverageReporter 中添加一个 cobertura 到 karma.config.js

     coverageReporter:  { 
     ....
     reporters: [
         ...
         { type: 'cobertura' } // TO BE ADDED        
     ]
    

    }