pytest---钉钉机器人发送Allure测试报告
前言
# coding:utf-8 import pytest class TestCase: def test_01(self): print('---用例01---') assert 1 def test_02(self): print('---用例02---') assert 0 def test_03(self): print('---用例03---') assert 1 def test_04(self): print('---用例04---') assert 0
pytest test03.py --alluredir ./report/result
allure generate report/result/ -o report/html --clean
{ "reportName" : "Allure Report", "testRuns" : [ ], "statistic" : { "failed" : 2, "broken" : 0, "skipped" : 0, "passed" : 2, "unknown" : 0, "total" : 4 }, "time" : { "start" : 1678778457211, "stop" : 1678778457313, "duration" : 102, "minDuration" : 0, "maxDuration" : 10, "sumDuration" : 12 } }
# allure报告结果json路径,这里小编直接将路径写死了 file_name = r'D:\test_daily\report\html\widgets\summary.json' with open(file_name, 'r', encoding='utf-8') as f: data = json.load(f) case_json = data['statistic'] case_all = case_json['total'] # 测试用例总数 case_fail = case_json['failed'] # 失败用例数量 case_pass = case_json['passed'] # 成功用例数量 if case_all >= 0: # 计算出来用例执行成功率 case_rate = round((case_pass / case_all) * 100, 2) else: print('未获取到执行结果')
def allure_report(): # 运行测试用例, os.system('pytest test03.py --alluredir ./report/result ') # 生成allure报告 os.system('allure generate report/result/ -o report/html --clean') # 自动打开allure报告,固定端口号 os.system('allure serve report/result -p 8210')
def send_ding(): # allure报告结果json路径 file_name = r'D:\test_daily\report\html\widgets\summary.json' with open(file_name, 'r', encoding='utf-8') as f: data = json.load(f) case_json = data['statistic'] case_all = case_json['total'] # 测试用例总数 case_fail = case_json['failed'] # 失败用例数量 case_pass = case_json['passed'] # 成功用例数量 if case_all >= 0: # 计算出来当前失败率 case_rate = round((case_pass / case_all) * 100, 2) else: print('未获取到执行结果') # 发送报告内容 text = f"用例通过率:{case_rate}%" \ f"\n执行用例数:{case_all}个" \ f"\n成功用例数:{case_pass}个" \ f"\n失败用例数:{case_fail}个" \ # 这里的路径是我本地的路径,如果项目在服务器上运行,可以更改为服务器地址 f"\n测试报告地址:http://10.72.5.82:9999/index.html" data = {"msgtype": "text","text": {"content":"%s"%text}, "at": { # 要@的人 "atMobiles": " ", # 是否@所有人 "isAtAll": False } } url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxx' headers = {'Content-Type': 'application/json'} r = requests.post(url,headers=headers,data=json.dumps(data)) print(r.status_code)
import requests import os import json import multiprocessing import time def allure_report(): # 运行测试用例, os.system('pytest test03.py --alluredir ./report/result ') # 生成allure报告 os.system('allure generate report/result/ -o report/html --clean') # 自动打开allure报告 os.system('allure serve report/result -p 4545') def send_ding(): # allure报告结果json路径 file_name = r'D:\test_daily\report\html\widgets\summary.json' with open(file_name, 'r', encoding='utf-8') as f: data = json.load(f) case_json = data['statistic'] case_all = case_json['total'] # 测试用例总数 case_fail = case_json['failed'] # 失败用例数量 case_pass = case_json['passed'] # 成功用例数量 if case_all >= 0: # 计算出来当前失败率 case_rate = round((case_pass / case_all) * 100, 2) else: print('未获取到执行结果') # 发送报告内容 text = f"用例通过率:{case_rate}%" \ f"\n执行用例数:{case_all}个" \ f"\n成功用例数:{case_pass}个" \ f"\n失败用例数:{case_fail}个" \ f"\n测试报告地址:http://10.72.5.82:4545/index.html" data = {"msgtype": "text","text": {"content":"%s"%text}, "at": { # 要@的人 "atMobiles": " ", # 是否@所有人 "isAtAll": False } } url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx' headers = {'Content-Type': 'application/json'} r = requests.post(url,headers=headers,data=json.dumps(data)) print(r.status_code) if __name__ == '__main__': # 执行生成allure报告 a = multiprocessing.Process(target=allure_report) # 执行发送钉钉机器人 b = multiprocessing.Process(target=send_ding) a.start() time.sleep(5) b.start()