當我們的接口測試用例腳本達到一定的數量后,就要想辦法持續集成化,持續集成的好處可以根據我們的觸發策略來構建相應的用例,譬如:定時構建、版本發布定時構建。本文采取的持續集成方案還是主流的Jenkins搭配github的方式。
主要思路
- 代碼倉庫保存api測試代碼
- Jenkins拉取代碼
- shell命令構建代碼
- 執行后生成allure報告
- 執行失敗或異常推送企業微信
Jenkins配置
倉庫拉取配置
image.png
構建定時配置
image.png
執行腳本配置
image.png
- 激活python虛擬環境
- 切換測試環境變量
- 執行指定腳本,并生成allure報告
生成測試報告
image.png
失敗觸發通知
image.png
腳本說明
觸發通知接口實現
class APITestRobotView(APIView):
def post(self, request, *args, **kwargs):
job_name = request.data.get("job", "未填寫")
tester = request.data.get("tester", "杰克克")
tester_ = settings.NAME_OF_ID.get(tester,tester)
test_env = request.data.get("env", "test92")
message_template = request.data.get("template", "api")
hook = request.data.get("hook",
"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1701dfba-c653-4a91-aaf2-a4eedd6eb3f3")
api_content = f"API接口測試自動化構建<font color=\"warning\">異常</font>通知,請相關同事注意。 \n\n" \
f">項目名稱:{job_name} \n " \
f">運行環境:{test_env} \n " \
f">跟蹤人負責人:{tester} \n" \
f"[詳情點擊](http://192.168.1.99:8021)\n" \
f"<@{tester_}>"
ui_content = f"UI測試自動化構建<font color=\"warning\">異常</font>通知,請相關同事注意。 \n\n" \
f">項目名稱:{job_name} \n " \
f">運行環境:{test_env} \n " \
f">跟蹤人負責人:{tester} \n" \
f"[詳情點擊](http://192.168.1.99:8021)\n" \
f"<@{tester_}>"
if message_template == 'api':
default_content = api_content
else:
default_content = ui_content
# 構建message
markdown_content = request.data.get("content", default_content)
content = {
"msgtype": "markdown",
"markdown": {
"content": markdown_content
}
}
resp = requests.post(url=hook, json=content)
return Response(resp.json())
if __name__ == '__main__':
pass
觸發通知接口調用
curl -X "POST" "http://192.168.1.99:8000/robot/apimessage/" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"tester": "tester",
"hook": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1701dfba-c653-4a91-aaf2-a4e2cd6eb3f3",
"env": "92測試環境",
"job": "apitest-dlvopenapi",
"template":"api"
}'