-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
81 lines (66 loc) · 2.9 KB
/
run.py
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
# encoding=utf-8
#!/usr/bin/env python
import fire
from unittest import defaultTestLoader
from core.config import Config
from core.logger import logger
from core.utils import big_camel_case
import datetime
from os import path
from XTestRunner import HTMLTestRunner
class Run():
'''UI自动化测试框架'''
def new_page(self, name):
'''根据模版生成一个测试用例,传入name参数指定测试用例名,以下划线形式。
eg: python run.py new_page <name>
'''
class_name = big_camel_case(name)
class_name = f"{class_name}Page"
# 页面文件
page_path = path.join(Config.PageDirPath, f"{name}.py")
with open(Config.PageTemplatePath, encoding="utf-8") as pf:
content = pf.read()
content = content.replace('__PageTemplate__', class_name)
with open(page_path, "w", encoding="utf-8") as cf:
cf.write(content)
cf.close()
pf.close()
logger.info(f'生成页面文件: {page_path}')
def new_case(self, name):
'''根据模版生成一个测试用例,传入 name 参数指定测试用例名,以下划线形式。
eg: python run.py new_case <name>
'''
class_name = big_camel_case(name)
class_name = f"Test{class_name}"
# 测试用例文件
case_path = path.join(Config.CaseDirPath, f"test_{name}.py")
with open(Config.TestCaseTemplatePath, encoding="utf-8") as pf:
content = pf.read()
content = content.replace('__CaseTemplate__', class_name)
with open(case_path, "w", encoding="utf-8") as cf:
cf.write(content)
cf.close()
pf.close()
logger.info(f'生成测试用例文件: {case_path}')
def start(self):
'''开始测试。eg: python run.py start'''
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
suite = defaultTestLoader.discover(Config.CaseDirPath,
pattern="test_*.py")
report_path = path.join(Config.ReportOutput, f"report_{now}.html")
with (open(report_path, 'wb')) as fp:
runner = HTMLTestRunner(stream=fp,
title=Config.ReportTitle,
description=Config.ReportDesc,
tester=Config.ReportTester,
language='zh-CN',
verbosity=2)
runner.run(testlist=suite, rerun=Config.Rerun, save_last_run=False)
logger.debug(f"生成测试报告:{report_path}")
def showconfig(self):
'''查看所有的配置。eg: python run.py show'''
for key, value in vars(Config).items():
if not key.startswith("__"):
print(f"{key}: {value}")
if __name__ == '__main__':
fire.Fire(Run)