forked from PAWPAW/lib_rgb
82 lines
3.1 KiB
Python
Executable File
82 lines
3.1 KiB
Python
Executable File
import time
|
|
from _pytest import terminal
|
|
import os
|
|
import pytest
|
|
import sys
|
|
from pawpaw_ci_test_framework.check import Check
|
|
from pawpaw_ci_test_framework.logger_ext import Logger
|
|
|
|
|
|
# 获取当前文件的绝对路径
|
|
current_file_path = os.path.abspath(__file__)
|
|
|
|
# 获取当前文件的上上上一级目录路径
|
|
grandparent_directory = os.path.dirname(os.path.dirname(os.path.dirname(current_file_path))).replace('\\', '/')
|
|
|
|
|
|
|
|
# 测试结果路径
|
|
test_result_file_path = os.path.join(grandparent_directory, "reports", "test_result", "test_result.txt")
|
|
|
|
|
|
def pytest_terminal_summary(terminalreporter, exitstatus, config):
|
|
# 收集测试结果
|
|
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'])
|
|
# total = terminalreporter._numcollected
|
|
total = passed + failed + error + skipped
|
|
success_rate = passed / (total - skipped) * 100
|
|
failure_rate = failed / (total - skipped) * 100
|
|
error_rate = error / (total - skipped) * 100
|
|
skip_rate = skipped / total * 100
|
|
# terminalreporter._sessionstarttime 会话开始时间
|
|
duration = time.time() - terminalreporter._sessionstarttime
|
|
# print('total times: %.2f' % duration, 'seconds')
|
|
|
|
failure_case_list = []
|
|
if failed == 0:
|
|
failure_case_list = []
|
|
else:
|
|
for rep in terminalreporter.stats.get('failed', []):
|
|
file_path = rep.nodeid.split("::")[0]
|
|
failure_case_list.append(file_path)
|
|
|
|
with open(test_result_file_path, "w") as fp:
|
|
fp.write("TOTAL=%s" % total + "\n")
|
|
fp.write("PASSED=%s" % passed + "\n")
|
|
fp.write("FAILED=%s" % failed + "\n")
|
|
fp.write("ERROR=%s" % error + "\n")
|
|
fp.write("SKIPPED=%s" % skipped + "\n")
|
|
fp.write("SUCCESS_RATE=%.2f%%" % success_rate + "\n")
|
|
fp.write("FAILURE_RATE=%.2f%%" % failure_rate + "\n")
|
|
fp.write("ERROR_RATE=%.2f%%" % error_rate + "\n")
|
|
fp.write("SKIP_RATE=%.2f%%" % skip_rate + "\n")
|
|
fp.write("TOTAL_TIMES=%.2fs" % duration + "\n")
|
|
fp.write("FAILURE_CASE_PATH=%s" % failure_case_list + "\n")
|
|
fp.close()
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
def get_logger_check(request):
|
|
filename = request.node.name
|
|
logger = Logger(name=filename)
|
|
check = Check(logger)
|
|
request.cls.logger = logger
|
|
request.cls.check = check
|
|
|
|
|
|
# 添加命令行参数为功能测试
|
|
def pytest_addoption(parser):
|
|
parser.addoption("--function", action="store_true")
|
|
|
|
@pytest.fixture
|
|
def function(pytestconfig):
|
|
return pytestconfig.getoption("function")
|
|
|
|
@pytest.fixture
|
|
def function(pytestconfig):
|
|
# 使用 pytestconfig.getoption 方法尝试获取 'function' 参数
|
|
# 如果参数不存在,则返回一个默认值,例如 False
|
|
return pytestconfig.getoption("function", default=False) |