import unittest
import os, datetime, time
from BeautifulReport import BeautifulReport
from framework.excel_oprate import read_config
from tomorrow import threads
root_dir = os.path.dirname(os.path.abspath(__file__)).replace('\\', '/')
test_dir = root_dir + '/testcases'
report_dir = root_dir + '/test_report'
def add_cases():
discover = unittest.defaultTestLoader.discover(test_dir, 'test*.py', None)
testsuit01 = unittest.TestSuite()
testsuit02 = unittest.TestSuite()
table_datas = read_config()
for test01 in discover:
for test02 in test01:
for test03 in test02:
testsuit01.addTest(test03)
for case in testsuit01:
case01 = str(case)
case01 = case01.split('(')[0].strip()
for i in range(len(table_datas)):
case_name = table_datas[i]['測試用例']
if case_name == case01:
if table_datas[i]['是否執行'] == float(1.0):
testsuit02.addTest(case)
print('----->要運行的測試用例:')
for te in testsuit02:
print(te)
return testsuit02
@threads(2)
def run(test_suit):
now = datetime.datetime.now().strftime('%Y-%m-%d %H_%M_%S')
filename = '測試報告' + str(now)
result = BeautifulReport(test_suit)
result.report(filename=filename, description='測試報告',
log_path=report_dir)
if __name__ == '__main__':
cases = add_cases()
for case in cases:
run(case)
或者如下:
import unittest import os, datetime from BeautifulReport import BeautifulReport from framework.excel_oprate import read_config from tomorrow import threads root_dir = os.path.dirname(os.path.abspath(__file__)).replace('\\', '/') test_dir = root_dir + '/testcases' report_dir = root_dir + '/test_report' def add_cases(): discover = unittest.defaultTestLoader.discover(test_dir, 'test*.py', None) testsuit01 = unittest.TestSuite() testsuit02 = unittest.TestSuite() table_datas = read_config() for test01 in discover: for test02 in test01: for test03 in test02: testsuit01.addTest(test03) for case in testsuit01: case01 = str(case) case01 = case01.split('(')[0].strip() for i in range(len(table_datas)): case_name = table_datas[i]['測試用例'] if case_name == case01: if table_datas[i]['是否執行'] == float(1.0): testsuit02.addTest(case) print('----->要運行的測試用例:') for te in testsuit02: print(te) return testsuit02 @threads(5) def run(test_suit): filename = '測試報告' result = BeautifulReport(test_suit) result.report(filename=filename, description='測試報告', log_path=report_dir) if __name__ == '__main__': now = datetime.datetime.now().strftime('%Y-%m-%d %H_%M_%S') report_name = '測試報告' + str(now) + '.html' cases = add_cases() for case in cases: result = run(case) os.chdir(report_dir) if os.path.exists('測試報告.html'): os.rename('測試報告.html', report_name)