码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-14 18:45:16      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:定义   tin   .com   glob   str   file   sci   find   report   

# 调试了一下午跟路径拆分逻辑有关的东西,脑阔疼,代码能力薄弱的一批, 还有前人栽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)

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

标签:定义   tin   .com   glob   str   file   sci   find   report   

原文地址:https://www.cnblogs.com/mariahcat/p/9310544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!