pytest-常用钩子函数
result = {"total": 0, "passed": 0, "failed": 0, "skipped": 0, "error": 0, "timeout": 0, "times": 0, "warnings": 0, "other": 0, "failures_list": [], "errors_list": [], "time_out_list": []} def pytest_terminal_summary(terminalreporter, exitstatus, config): """ 统计测试结果并发送到钉钉 :param terminalreporter: :param exitstatus: :param config: :return: """ # print(terminalreporter.stats) fail_type = [] save_data = [] now = time.time() total = terminalreporter._numcollected passed = len([i for i in terminalreporter.stats.get('passed', []) if i.when != 'teardown']) failed = len([i for i in terminalreporter.stats.get('failed', []) if i.when != 'teardown']) error = len([i for i in terminalreporter.stats.get('error', []) if i.when != 'teardown']) skipped = len([i for i in terminalreporter.stats.get('skipped', []) if i.when != 'teardown']) print({"total":total,"failed":failed,"error":error,"skipped":skipped}) result.update({"total": total}) result.update({"passed": passed}) result.update({"failed": failed}) result.update({"error": error}) result.update({"skipped": skipped}) for success in terminalreporter.stats.get('passed', []): if success.when != 'teardown': try: desc_dict = eval(success.description) # print("0000000:", desc_dict) url = desc_dict["url"] path = get_url_path(url) except (Exception,): path = "/no" t = success.duration if t >= TIME_OUT/1000: suite = success.fspath suite_name = suite.split("/")[1] if suite_name not in fail_type: fail_type.append(suite_name) result["timeout"] += 1 t_str = {"path": path, "sec": round(t, 2)} result["time_out_list"].append(t_str) if t >= 5: suite = success.fspath suite_name = suite.split("/")[1] t_str = {"project": suite_name, "path": path, "sec": int(t*1000), "type": 0, "datetime": now} save_data.append(t_str) for failed in terminalreporter.stats.get('failed', []): if failed.when != 'teardown': suite = failed.fspath suite_name = suite.split("/")[1] if suite_name not in fail_type: fail_type.append(suite_name) try: desc_dict = eval(failed.description) url = desc_dict["url"] path = get_url_path(url) code = desc_dict["code"] rsp = desc_dict["rsp"] except (Exception,): path = "/no" code = "0" rsp = "未知内容" f_str = {"project": suite_name, "path": path, "code": code, "rsp": rsp, "type": 1, "datetime": now} result["failures_list"].append(f_str) save_data.append(f_str) duration = time.time() - terminalreporter._sessionstarttime result["times"] = round(duration, 2) if result["failed"] > 0 or result["error"] > 0 or result["timeout"] > 0: title_type = "|".join(fail_type) if MSG_SWITCH and platform.system() == "Linux": # for token in DING_TOKEN: # push_ding_msg(title_type, token, result, REPORT_PATH) for token in WX_TOKEN: push_wx_msg(title_type, token, result, REPORT_PATH) for token in FS_TOKEN: push_fs_msg(title_type, token, result, REPORT_PATH) if save_data: save_to_mongodb(save_data) if PHONE_SWITCH and platform.system() == "Linux": res = jude_phone_notice(MAX_ERROR, API_LIST) phone_notice(res, DUTY_PHONE, PHONE_DELAY_TIME)
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
当测试失败的时候,自动截图,展示到html报告中
** 作者:上海-悠悠 QQ交流群:588402570**
:param item:
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call' or report.when == "setup":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
file_name = report.nodeid.replace("::", "_") + ".png"
screen_img = _capture_screenshot()
if file_name:
html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % screen_img
extra.append(pytest_html.extras.html(html))
report.extra = extra