摆脱京城贵妇unittest的骚套路discover,自定义用例执行顺序。

# 调试了一下午跟路径拆分逻辑有关的东西,脑阔疼,代码能力薄弱的一批, 还有前人栽htmltestrunner后入乘凉的感觉不要太爽

# 用TestLoader().discover()找到的用例添加进testsuite中执行顺序是按照ASCII顺序来搞得,所以写了这个找固定目录及子目录下的所有固定格式case并自动导入

def find_case(case_path):
if os.path.exists(case_path):
script_list = os.listdir(case_path)
for script in script_list:
testScript = os.path.join(case_path, script)
if os.path.isdir(testScript):
find_case(testScript)
elif script[0:3] == "NO_" and script[-3:] == ".py":
script_dir = os.path.dirname(testScript)
script_path = ".".join(script_dir.split("\\")[2:])
path = "from " + script_path + " import "
exec_str = path + script[:-3]
exec(exec_str, globals())
case = "%s.%s('%s')" % (script[:-3], script[:-3], "test_procedure")
case_list.append(case)
return case_list
def get_suite(case_path):
cases = find_case(case_path)
for case in cases:
suite.addTest(eval(case))
return suite

 pasting

def TestSuitRunner(rootDir=CASE_PATH,
Report_Name='XXX-X-V2.0 AutoTestReport.html',
tester='Mariahcat'):
test_suite = get_suite(rootDir)
filePath = os.path.join(REPORT_PATH, Report_Name)
fp = open(filePath, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=Report_Name,
tester=tester
)
runner.run(test_suite)
fp.close()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/14 16:09
# @Author : Mariahcat
# @Desc :

from ATFactory.Common.ParameterFactory.Config import Config, CONFIG_FILE
from ATFactory.Common.ReportFactory import TestSuitFactory

config = Config(config=CONFIG_FILE)
REPORT_INFO = config.get("REPROT INFO")
test_dir = REPORT_INFO.get('TEST_DIR')
report_name = REPORT_INFO.get('REPORT_NAME')
tester = REPORT_INFO.get('TESTER')

TestSuitFactory.TestSuitRunner(rootDir=test_dir, Report_Name=report_name, tester=tester)
posted @ 2018-07-14 18:27  Mariahcat  阅读(548)  评论(0编辑  收藏  举报