使用 Jenkins 从 Karma.js 获取百分比覆盖率

Getting Percentage coverage from Karma.js with Jenkins

我 运行 karma.js 负责单元测试和与 jenkins 管道的集成。我的目标是读取 Karma 抛出的错误类型,如果它是与我想终止作业的百分比相关的错误,否则即使存在单元测试失败等其他错误也要继续(这是一个要求并且有原因为此。)

我没有找到执行此操作的方法。任何想法表示赞赏!

karma start ibx-test/olb/karma.conf.js --browsers PhantomJS --log-level warn --single-run


coverageReporter: {
        type: 'lcov',
        dir: 'unit-tests/coverage/',
            check: {
                global: {
                lines: 100 //This is just for testing
                }
            }
    }

16:17:43 [Unit Test] 09 03 2017 21:17:43.024:ERROR [coverage]: PhantomJS 2.1.1 (Linux 0.0.0): Coverage for lines (90.33%) does not meet global threshold (100%)

编辑:我在 "Build step" 下的管道语法中找到了 "Process xUnit test result report",我能以某种方式使用它吗?业力报告与 xUnit 之间是否存在相关性?

我找到了一种方法来做到这一点。 "Process xUnit test result report" 有助于做到这一点。我检查了管道语法,它给了我下面的脚本并且它有效。

step([$class: 'XUnitBuilder', testTimeMargin: '3000', thresholdMode: 1, 
    thresholds: [
     [$class: 'FailedThreshold', failureNewThreshold: '', 
              failureThreshold: '2', unstableNewThreshold: '', 
              unstableThreshold: '1'], 
     [$class: 'SkippedThreshold', failureNewThreshold: '', 
            failureThreshold: '', unstableNewThreshold: '', 
            unstableThreshold: '']], 
    tools: [[$class: 'JUnitType', deleteOutputFiles: false, 
      failIfNotNew: false, pattern: 'ibx-test/reports/unit-tests/PhantomJS_2.1.1_(Linux_0.0.0)/ibx-test/reports/unit-tests/*.xml',
      skipNoTestFiles: false, stopProcessingIfError: false]]])
  • thresholdMode:表示(失败或跳过)测试的数量将用于阈值。 1 代表数字,2 代表百分比。我使用了 1,所以我可以让一个测试失败,我得到了想要的结果。
  • FailedThreshold:是用于失败阈值的 class。
  • SkippedThreshold:可用于跳过的测试。我还没用

到目前为止,我还没有关注此测试的其他参数。

如您所见,我的值为 2(failureThreshold:'2')。一旦我有 2 个测试失败,构建就会失败并终止。