最近打算写一下自动化结果(UT和AT)的DashBoard(显示一下微服务一段时间的结果),所以需要写个小脚本来做一些事情。
首先要得到jenkins job中的console 的结果,我使用的是通过jenkins的api进行得到结果:
如何得到API Token: 在Jenkins登录后右上角账户 --》 设置 --》点击 show API Token
- 得到job最新的build number
- 得到对应job的console log,然后进行正则表达式得到对应job的UT & AT的行覆盖率和类覆盖率
# coding:utf-8
import jenkins
import re
job_names = ['store/store-outer/store-openapi-build',
'store/store-outer/store-openapi-zbrd-auto']
username = 'xxxx'
password = '0e10e619c76a35513ec5a4bfe2b0d5c4'
class Jenkins_Tool():
def __init__(self):
self.jenkins_url = 'http://jenkins.xxxx.com/jenkins/'
self.sever = jenkins.Jenkins(self.jenkins_url, username=username, password=password)
def get_build_number(self, job_name):
return (self.sever.get_job_info(job_name)['lastCompletedBuild']['number'])
def get_build_console_output(self, job_name):
number = self.get_build_number(job_name)
line_coverage = 0
code_coverage = 0
resps= (self.sever.get_build_console_output(job_name, number))
line_code = (re.search(r'(line:)\s(\d+)', resps, re.M))
class_code = (re.search(r'(class:)\s(\d+)', resps, re.M))
if line_code and class_code:
line_coverage = line_code.group(2)
code_coverage = class_code.group(2)
print "行覆盖率为:{0}".format(line_coverage)
print "类覆盖率为:{0}".format(code_coverage)
test = Jenkins_Tool()
for job_name in job_names:
test.get_build_console_output(job_name)
时间比较紧,所以就写了点点,不过可以通过上面的代码得到对应jenkins job的console log了,然后通过正则得到想要得到的值,以后再进行优化吧!