背景簡(jiǎn)述
結(jié)合前面三篇文章
【python3-1】讀取jmeter報(bào)告內(nèi)容
【python3-2】讀取html報(bào)告返回值,作為接口傳參調(diào)用
【python3-3】argparse命令行添加參數(shù)
本章主要是通過Jenkins集成腳本進(jìn)行自動(dòng)化執(zhí)行測(cè)試
Jenkins集成腳本
新建 流水線(Pipeline)工程

流水線(Pipeline)工程
配置基礎(chǔ)配置,保存7天,保留6條記錄

基礎(chǔ)配置
配置參數(shù)化構(gòu)建過程,添加變量參數(shù)化,可以選擇和填寫變量

參數(shù)化構(gòu)建

參數(shù)化構(gòu)建

參數(shù)化構(gòu)建

參數(shù)化構(gòu)建

參數(shù)化構(gòu)建
編輯Pipline腳本
參考pipline官網(wǎng)
參見郵件配置說明Jmeter接口測(cè)試(十三)Jenkins_Pipeline郵件配置
node('master') {
stage('Init'){
git 'git@gitlab.egomsl.com:dz-qa/auto_api.git'
}
stage('Excute'){
echo '開始執(zhí)行腳本'
withAnt{
sh 'ant -file /var/lib/jenkins/workspace/AutoTest-soa-${parameter}/conf/${parameter}_build.xml'
}
}
stage('Deployment Init') {
script {
sh 'ls /opt/htdocs/jenkins/reports/soa/html/${parameter}/ | egrep *.html > cart_listFiles.txt'
def cart_files = readFile("cart_listFiles.txt").split("\\r?\\n");
sh 'rm -f ${parameter}_listFiles.txt'
for (i = 0; i < cart_files.size(); i++) {
publishHTML target: [
allowMissing:false,
alwaysLinkToLastBuild: false,
keepAll:true,
reportDir: '/opt/htdocs/jenkins/reports/soa/html/${parameter}/',
reportFiles: cart_files[i],
reportName: 'HTML Report'
]
}
}
}
stage('Report&Email'){
echo '獲取測(cè)試報(bào)告統(tǒng)計(jì)'
withAnt{
sh 'python3 /var/lib/jenkins/workspace/AutoTest-${parameter_name}/py-api/demo01.py -v1=${parameter} -v2=${parameter_name} -v3=${execute_type} -v4=${execute_stage}'
echo '發(fā)送報(bào)告'
emailext (
body: '''<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
<table width="95%" cellpadding="0" cellspacing="0" style="font-size: 16pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
<tr>
<td><br />
<b><font color="#0B610B"><font size="6">構(gòu)建信息</font></font></b>
<hr size="2" width="100%" align="center" /></td>
</tr>
<tr>
<td>
<ul>
<div style="font-size:18px">
<li>構(gòu)建名稱:${PROJECT_NAME}</li>
<li>構(gòu)建結(jié)果: <span style="color:green"> Successful </span></li>
<li>構(gòu)建編號(hào):${BUILD_NUMBER}</li>
<li>觸發(fā)原因:${CAUSE}</li>
<li>部署分支:${gitBranch}</li>
<li>構(gòu)建地址:<a href=${BUILD_URL}>${BUILD_URL}</a></li>
<li>構(gòu)建日志:<a href=${BUILD_URL}console>${BUILD_URL}console</a></li>
<li>環(huán)境: ${environment}</li>
<li>測(cè)試階段:${execute_stage}</li>
<li>變更概要:${CHANGES}</li>
<li>測(cè)試報(bào)告地址:<a href=${BUILD_URL}HTML_20Report>${BUILD_URL}HTML_20Report</a></li>
<li>變更集:${JELLY_SCRIPT}</li>
<li><font color="#0B610B"><font size="6">測(cè)試結(jié)果報(bào)告</font></font> ${FILE, path="/opt/htdocs/jenkins/reports/soa/html/${parameter}/${parameter_name}_Report.html"}
</div>
</ul>
</td>
</tr>
</table>
</body>
</html>''',
subject: '${gitName}執(zhí)行結(jié)果:Successful - ${PROJECT_NAME} - Build # ${BUILD_NUMBER} !',
to: '${noticeEmail}',
)
}
}
}
配置完成,應(yīng)用保存退出!
python腳本集成
把已經(jīng)完成的腳本,通過命令行方式,把外部變量參數(shù)引入腳本內(nèi)調(diào)用,方法是調(diào)用argparse庫(kù)(【python3-3】argparse命令行添加參數(shù))
import urllib
import http.cookiejar
import requests
import json
import argparse
import sys,re
import time
from urllib import request
from bs4 import BeautifulSoup
'''引入外部參數(shù)'''
if __name__ == "__main__":
# 創(chuàng)建命令行解析器句柄,并自定義描述信息
parser = argparse.ArgumentParser(description="test the argparse package")
# 定義必選參數(shù) positionArg
# parser.add_argument("project_name")
# 定義可選參數(shù)module
parser.add_argument("--module", "-v1", help="模塊")
# 定義可選參數(shù)case_suit_name
parser.add_argument("--case_suit_name", "-v2", help="模塊名稱")
# 定義可選參數(shù)execute_type
parser.add_argument("--execute_type", "-v3", help="CI/Normal")
# 定義可選參數(shù)execute_stage
parser.add_argument("--execute_stage", "-v4", help="FeatureTest / RegressionTest / PreReleaseTest / ReleaseTest")
args = parser.parse_args() # 返回一個(gè)命名空間
print(args)
params = vars(args) # 返回 args 的屬性和屬性值的字典
parameter=[]
for k, v in params.items():
parameter.append(v)
# print(v)
print(parameter[0])
print(parameter[1])
print(parameter[2])
print(parameter[3])
'''獲取報(bào)告返回?cái)?shù)值'''
path = '/opt/htdocs/jenkins/reports/soa/html/'+parameter[0]+'/'+parameter[1]+'_Report.html'
with open(path, 'r',encoding='utf-8') as f:
Soup = BeautifulSoup(f.read(), 'lxml')
titles = Soup.select('html > body > table > .Failure > td')
lists = []
for title in titles:
lists.append(title.text)
print("api_total:",lists[0],",","api_pass_total:",lists[1],",","api_pass_rate:",lists[3],",","excute_time:",lists[7])
'''轉(zhuǎn)換時(shí)間'''
ms = lists[7]
listss =re.compile(r'\d+').findall(ms)
ss = int(listss[0])/1000 #毫秒轉(zhuǎn)為秒
print(r'將毫秒轉(zhuǎn)換為秒:',ms,'-->',ss,'s')
'''執(zhí)行接口調(diào)用方法'''
test_time = time.strftime ("%Y-%m-%d %H:%M:%S", time.localtime ()) #獲取當(dāng)前時(shí)間
url = " http://godeye.hqygou.com/api/receiveAutoTestResultTest"
# postdata =urllib.parse.urlencode({
header = {"Content-Type":"application/json"}
raw={
"api_key": "2b0e740f99f20906a54d04ebe9816d9b",
"api_sign": "sTest123!@#",
"project_id": 10007,
"project_name": "SOA",
"platform": "OTHER",
"level": "Core",
"case_total": 0,
"case_pass_total": 0,
"case_pass_rate": 0,
"api_total": lists[0],
"api_pass_total": lists[1],
"api_pass_rate": lists[3],
"execute_detail": [{
"case_suit_id": "1",
"case_suit_name": parameter[1],
"module": parameter[0],
"case_total": "0",
"case_pass_total": "0",
"case_pass_rate": "0",
"api_total": lists[0],
"api_pass_total": lists[1],
"api_pass_rate": lists[3]
}],
"execute_stage": parameter[3],
"execute_scene": "BaselineRelease",
"execute_type": parameter[2],
"autotest_type": "Interface",
"autotest_source": "Jmeter",
"excute_start_time": test_time,
"excute_end_time": test_time,
"excute_time": ss,
"report_url": "test",
"test_result": 1,
"execute_result": 1,
"desc": ""
}
data = json.dumps(raw)
data1 = bytes(data,"utf8")
print(data1.decode('unicode_escape'))
req = urllib.request.Request(url,data1,header)
# print(urllib.request.urlopen(req).read().decode('utf-8'))
# #自動(dòng)記住cookie
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
r = opener.open(req)
print(r.read().decode('unicode_escape'))
保存腳本,上傳到git庫(kù)
Jenkins執(zhí)行自動(dòng)化結(jié)果
Jenkins執(zhí)行界面進(jìn)度結(jié)果

執(zhí)行進(jìn)度
查看測(cè)試報(bào)告

測(cè)試報(bào)告
查看控制臺(tái)(Console Output)執(zhí)行日志
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/AutoTest-soa-goods
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Init)
[Pipeline] git
Cloning the remote Git repository
Cloning repository git@gitlab.egomsl.com:xxxx/auto_api.git
> git init /var/lib/jenkins/workspace/AutoTest-soa-goods # timeout=10
Fetching upstream changes from git@gitlab.egomsl.com:xxxx/auto_api.git
> git --version # timeout=10
> git fetch --tags --progress git@gitlab.egomsl.com:xxxx/auto_api.git +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url git@gitlab.egomsl.com:xxxx/auto_api.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url git@gitlab.egomsl.com:xxxx/auto_api.git # timeout=10
Fetching upstream changes from git@gitlab.egomsl.com:xxxx/auto_api.git
> git fetch --tags --progress git@gitlab.egomsl.com:xxxx/auto_api.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 72d35f846f005b7955e26442ce4a23683c0791a1 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 72d35f846f005b7955e26442ce4a23683c0791a1
> git branch -a -v --no-abbrev # timeout=10
> git checkout -b master 72d35f846f005b7955e26442ce4a23683c0791a1
Commit message: "更新配置文件"
> git rev-list --no-walk 261d9419bd5c8104662bbce83ec873f10b68c072 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Excute)
[Pipeline] echo
開始執(zhí)行腳本
[Pipeline] withAnt
[Pipeline] {
[Pipeline] sh
+ ant -file /var/lib/jenkins/workspace/AutoTest-soa-goods/conf/goods_build.xml
Buildfile: /var/lib/jenkins/workspace/AutoTest-soa-goods/conf/goods_build.xml
all:
商品:
[jmeter] Executing test plan: /var/lib/jenkins/workspace/AutoTest-soa-goods/soa-goods/soa-goods_2.jmx ==> /opt/htdocs/jenkins/reports/soa/jtl/goods/soa-goods_Report201903010745.jtl
[jmeter] Creating summariser <summary>
[jmeter] Created the tree successfully using /var/lib/jenkins/workspace/AutoTest-soa-goods/soa-goods/soa-goods_2.jmx
[jmeter] Starting the test @ Fri Mar 01 19:45:26 CST 2019 (1551440726705)
[jmeter] Waiting for possible Shutdown/StopTestNow/Heapdump message on port 4445
[jmeter] summary + 3 in 00:00:03 = 1.1/s Avg: 848 Min: 324 Max: 1648 Err: 1 (33.33%) Active: 1 Started: 1 Finished: 0
[jmeter] summary + 83 in 00:00:14 = 5.7/s Avg: 69 Min: 1 Max: 775 Err: 14 (16.87%) Active: 0 Started: 1 Finished: 1
[jmeter] summary = 86 in 00:00:17 = 5.0/s Avg: 96 Min: 1 Max: 1648 Err: 15 (17.44%)
[jmeter] Tidying up ... @ Fri Mar 01 19:45:44 CST 2019 (1551440744837)
[jmeter] ... end of run
report:
[xslt] Processing /opt/htdocs/jenkins/reports/soa/jtl/goods/soa-goods_Report201903010745.jtl to /opt/htdocs/jenkins/reports/soa/html/goods/soa-goods_Report.html
[xslt] Loading stylesheet /usr/local/jmeter/apache-jmeter-4.0/extras/jmeter-results-new-report.xsl
BUILD SUCCESSFUL
Total time: 22 seconds
[Pipeline] }
[Pipeline] // withAnt
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deployment Init)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ ls /opt/htdocs/jenkins/reports/soa/html/goods/
+ egrep '*.html'
[Pipeline] readFile
[Pipeline] sh
+ rm -f goods_listFiles.txt
[Pipeline] publishHTML
[htmlpublisher] Archiving HTML reports...
[htmlpublisher] Archiving at BUILD level /opt/htdocs/jenkins/reports/soa/html/goods to /var/lib/jenkins/jobs/AutoTest-soa-goods/builds/9/htmlreports/HTML_20Report
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Report&Email)
[Pipeline] echo
獲取測(cè)試報(bào)告統(tǒng)計(jì)
[Pipeline] withAnt
[Pipeline] {
[Pipeline] sh
+ python3 /var/lib/jenkins/workspace/AutoTest-soa-goods/py-api/demo01.py -v1=goods -v2=soa-goods -v3=Normal -v4=RegressionTest
Namespace(case_suit_name='soa-goods', execute_stage='RegressionTest', execute_type='Normal', module='goods')
goods
soa-goods
Normal
RegressionTest
api_total: 101 , api_pass_total: 78 , api_pass_rate: 77.23% , excute_time: 14254 ms
將毫秒轉(zhuǎn)換為秒: 14254 ms --> 14.254 s
b'{"api_key": "2b0e740f99f20906a54d04ebe9816d9b", "api_sign": "sTest123!@#", "project_id": 10007, "project_name": "SOA", "platform": "OTHER", "level": "Core", "case_total": 0, "case_pass_total": 0, "case_pass_rate": 0, "api_total": "101", "api_pass_total": "78", "api_pass_rate": "77.23%", "execute_detail": [{"case_suit_id": "1", "case_suit_name": "soa-goods", "module": "goods", "case_total": "0", "case_pass_total": "0", "case_pass_rate": "0", "api_total": "101", "api_pass_total": "78", "api_pass_rate": "77.23%"}], "execute_stage": "RegressionTest", "execute_scene": "BaselineRelease", "execute_type": "Normal", "autotest_type": "Interface", "autotest_source": "Jmeter", "excute_start_time": "2019-03-02 19:29:44", "excute_end_time": "2019-03-02 19:29:44", "excute_time": 14.254, "report_url": "test", "test_result": 1, "execute_result": 1, "desc": ""}'
{"code":200,"msg":"請(qǐng)求成功","result":{"base_result":"303","detail_result":["35"]}}
[Pipeline] echo
發(fā)送報(bào)告
[Pipeline] emailext
Sending email to: xxxx@gxxxxxxxx.com
[Pipeline] }
[Pipeline] // withAnt
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
執(zhí)行成功,并發(fā)送報(bào)告

郵件報(bào)告
查看數(shù)據(jù)庫(kù)生成數(shù)據(jù)

生成數(shù)據(jù)