我的项目有许多包(“模型”,“控制器”等)。我已经使用Cobertura插件设置了Jenkins以生成覆盖报告,这很棒。如果覆盖率低于某个阈值,我想将构建标记为不稳定,但仅限于某些软件包(例如,“控制器”,而不是“模型”)。我没有在配置UI中看到一种明显的方法,但看起来阈值是全局的。
有办法做到这一点吗?
答案 0 :(得分:2)
(在这里回答我自己的问题)
据我所知,这是不可能的 - 经过几天的观察,我没有看到任何东西。我写了一个简单的脚本,它可以做我想要的 - 获取覆盖输出,解析它,如果特定包的覆盖范围不符合特定阈值,则构建失败。它很脏,可以清理/扩展,但基本的想法就在这里。欢迎提出意见。
#!/usr/bin/env python
'''
Jenkins' Cobertura plugin doesn't allow marking a build as successful or
failed based on coverage of individual packages -- only the project as a
whole. This script will parse the coverage.xml file and fail if the coverage of
specified packages doesn't meet the thresholds given
'''
import sys
from lxml import etree
PACKAGES_XPATH = etree.XPath('/coverage/packages/package')
def main(argv):
filename = argv[0]
package_args = argv[1:] if len(argv) > 1 else []
# format is package_name:coverage_threshold
package_coverage = {package: int(coverage) for
package, coverage in [x.split(':') for x in package_args]}
xml = open(filename, 'r').read()
root = etree.fromstring(xml)
packages = PACKAGES_XPATH(root)
failed = False
for package in packages:
name = package.get('name')
if name in package_coverage:
# We care about this one
print 'Checking package {} -- need {}% coverage'.format(
name, package_coverage[name])
coverage = float(package.get('line-rate', '0.0')) * 100
if coverage < package_coverage[name]:
print ('FAILED - Coverage for package {} is {}% -- '
'minimum is {}%'.format(
name, coverage, package_coverage[name]))
failed = True
else:
print "PASS"
if failed:
sys.exit(1)
if __name__ == '__main__':
main(sys.argv[1:])