-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
59 lines (52 loc) · 2.1 KB
/
run.py
File metadata and controls
59 lines (52 loc) · 2.1 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
import os
import argparse
import subprocess
from utils.logger import logger
def run_tests(env="test", markers=None, parallel=1, allure_report=False):
"""
运行测试用例
:param env: 运行环境,默认test
:param markers: 测试标记,如"smoke and high"
:param parallel: 并行数,默认1
:param allure_report: 是否生成allure报告,默认False
"""
try:
# 设置环境变量
os.environ["TEST_ENV"] = env
logger.info(f"开始在{env}环境执行测试...")
# 构建命令
cmd = ["pytest"]
if markers:
cmd.extend(["-m", markers])
if parallel > 1:
cmd.extend(["-n", str(parallel)])
# 执行命令
result = subprocess.run(cmd, check=True)
# 生成allure报告
if allure_report:
logger.info("生成Allure报告...")
subprocess.run(
["allure", "generate", "reports/allure_results", "-o", "reports/allure_report", "--clean"],
check=True
)
logger.info("Allure报告已生成: reports/allure_report/index.html")
logger.info("测试执行完成")
return result.returncode
except subprocess.CalledProcessError as e:
logger.error(f"测试执行失败: {str(e)}")
return e.returncode
except Exception as e:
logger.error(f"执行测试时发生错误: {str(e)}")
return 1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="运行接口测试")
parser.add_argument("--env", type=str, default="test",
help="测试环境: dev, test, prod")
parser.add_argument("--markers", type=str,
help="测试标记,如'smoke and high'")
parser.add_argument("--parallel", type=int, default=1,
help="并行执行的进程数")
parser.add_argument("--allure", action="store_true",
help="生成Allure报告")
args = parser.parse_args()
run_tests(args.env, args.markers, args.parallel, args.allure)