-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.py
More file actions
115 lines (106 loc) · 3.57 KB
/
main.py
File metadata and controls
115 lines (106 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import sys
from datetime import datetime
from pathlib import Path
import click
import pytest
from playhouse.shortcuts import model_to_dict
from conf.config import settings
from src.model.auto_pytest import (
CaseFunc,
CaseMoudle,
CaseTag,
Project,
Suite,
TestPlan,
TestResult,
)
from src.model.modelsbase import database
from src.utils.file_operation import file_opreator
from src.utils.log_moudle import logger
# pytest.main(["-s", "-v", "tests/test_goods/test_good_add_del.py","tests/test_goods/test_update_good.py"])
@click.command()
@click.option(
"--cases",
type=str,
help="指定测试用例",
)
@click.option(
"--allure_dir",
default="./reports",
help="指定测试报告",
)
@click.option("--result_id", type=int, help="唯一标识,用于存储测试相关数据")
def run_pytest(cases: str, allure_dir: str, result_id: str):
cwd_path = str(Path().cwd())
logger.info(cwd_path)
logger.info(settings.nginx.pytest_static_dir)
nginx_url = settings.nginx.static_url
# 连接数据库
database.connect()
result = TestResult.get(id=result_id)
logger.info(
f"开始执行测试用例:{cases.split(',')},测试报告路径:{allure_dir.split(',')}"
)
# 如果case和allure_dir参数为空,则抛出异常
if cases == "" and allure_dir == "":
raise Exception("请输入测试用例或测试报告路径")
# 如果case参数为空,则执行所有测试用例
if cases == "":
ret_code = pytest.main(["-s", "-v", "--alluredir", allure_dir])
# 如果allure_dir参数为空,则执行指定测试用例
elif allure_dir == "":
ret_code = pytest.main(["-s", "-v", *cases.split(",")])
# 如果case和allure_dir参数都不为空,则执行指定测试用例并生成测试报告
else:
logger.info(
[
"-s",
"-v",
*cases.split(
sep=" ",
),
"--alluredir",
f"{allure_dir}/results",
]
)
ret_code = pytest.main(
[
"-s",
"-v",
*cases.split(
sep=" ",
),
"--alluredir",
f"{allure_dir}/results",
]
)
if ret_code == pytest.ExitCode.OK:
result.result = "PASS"
result.save()
else:
result.result = "FAIL"
result.save()
# 生成测试报告
os.system(f"allure generate {allure_dir}/results -o {allure_dir}/report --clean")
logger.info(f"测试报告已生成,路径为:{allure_dir}/report")
result.report_link = f"{allure_dir}/report/index.html".replace(settings.nginx.pytest_static_dir, nginx_url)
result.save()
# 打包测试相关的产物
file_opreator.tar_packge(
output_filename=f"{allure_dir}.tar.gz", source_dir=f"{allure_dir}"
)
report_path = f"{allure_dir}.tar.gz".replace(settings.nginx.pytest_static_dir, nginx_url)
logger.info(f"测试报告已打包,路径为:{report_path}")
result.report_download = report_path
result.save()
# 路径替换,前缀为settings.test.nginx_url的路径
# 测试完成
result.status = 1
result.save()
# 如果未关闭,关闭数据库连接
if not database.is_closed():
database.close()
## TODO 这部分缺少一个对比历史趋势的allure报告优化,可以参考allure的官方文档:https://allurereport.org/docs/history-and-retries/#how-to-enable-history 留作后期优化
if __name__ == "__main__":
run_pytest()