def test_case(self, case_value): # print('\nRunning Test Method: {}'.format(inspect.stack()[0][3])) do_log.info('\nRunning Test Method: {}'.format(inspect.stack()[0][3])) # data_namedtuple = cases_list.pop(0) case_id = case_value.case_id msg = case_value.title l_data = case_value.l_data r_data = case_value.r_data expect_result = case_value.expect_result actual_result = MathCalculate(l_data, r_data).divide() # 将实际结果写入excel # ws.cell(row=case_id + 1, column=6, value=actual_result) try: self.assertEqual(actual_result, expect_result, msg='测试{}失败'.format(msg)) except AssertionError as e: # print('具体异常为:{}'.format(e)) do_log.error('具体异常为:{}'.format(e)) # self.file.write('{},执行结果:{},具体异常为:{}\n'.format(msg, 'Fail', e)) # self.file.write('{},执行结果:{},具体异常为:{}\n'.format(msg, do_config('msg', 'fail_result'), e)) # ws.cell(row=case_id + 1, column=7, value='Fail') # self.handle_excel.write_result(case_id + 1, actual_result, 'Fail') do_excel.write_result(case_id + 1, actual_result, do_config('msg', 'fail_result')) raise e else: # self.file.write('{},执行结果:{}\n'.format(msg, 'Pass')) # self.file.write('{},执行结果:{}\n'.format(msg, do_config('msg', 'success_result'))) # ws.cell(row=case_id + 1, column=7, value='Pass') # self.handle_excel.write_result(case_id + 1, actual_result, 'Pass') do_excel.write_result(case_id + 1, actual_result, do_config('msg', 'success_result'))
def __init__(self): self.case_logger = logging.getLogger(do_config('log', 'logger_name')) self.case_logger.setLevel(do_config('log', 'logger_level')) console_output = logging.StreamHandler() file_output = RotatingFileHandler( filename=do_config('log', 'logger_name'), maxBytes=do_config('log', 'maxBytes'), backupCount=do_config('log', 'backupCount'), encoding='utf8') console_output.setLevel(do_config('log', 'console_level')) file_output.setLevel(do_config('log', 'file_level')) simple_formatter = logging.Formatter( do_config('log', 'simple_formatter')) verbose_formatter = logging.Formatter( do_config('log', 'verbose_formatter')) console_output.setFormatter(simple_formatter) file_output.setFormatter(verbose_formatter) self.case_logger.addHandler(console_output) self.case_logger.addHandler(file_output)
def write_result(self, row, actual, result): """ 将测试用例实际结果和通过结果保存到excel :param row: 行号 :param actual: 实际结果 :param result: 通过结果 :return: """ if isinstance(row, int) and (2 <= row <= self.ws.max_row): # self.ws.cell(row=row, column=6, value=actual) self.ws.cell(row=row, column=do_config('excel', 'actual_col'), value=actual) # self.ws.cell(row=row, column=7, value=result) self.ws.cell(row=row, column=do_config('excel', 'result_col'), value=result) self.wb.save(self.filename)
def write_result(self, row, actual, result): """ 将实际值与测试用例执行的结果保存到excel中 :param row: 保存到哪一行 :param actual: 实际值 :param result: 测试用例执行的结果,“Pass”, “Fail” :return: """ other_wb = load_workbook(self.filename) other_ws = other_wb[self.sheetname] if isinstance(row, int) and (2 <= row <= other_ws.max_row): other_ws.cell(row=row, column=do_config("excel", "actual_col"), value=actual) other_ws.cell(row=row, column=do_config("excel", "result_col"), value=result) other_wb.save(self.filename) other_wb.close() else: print("传入的行号有误,行号应为大于1的整数")
import unittest import inspect from Class_16_Homework.homework_0520.ddt import ddt, data # 导入ddt,ddt和data必须同时导入 # 导入数学计算的测试类 from Class_16_Homework.homework_0520.math_cal import MathCalculate # 导入Excel类 from Class_16_Homework.homework_0520.excel_class import HandleExcel # 导入配置文件类 from Class_16_Homework.homework_0520.config_class import do_config # 导入日志类 from Class_16_Homework.homework_0520.log_class import do_log do_excel = HandleExcel(do_config('file path', 'cases_path'), 'divide') @ddt # 在类的上一行加 class TestDivide(unittest.TestCase): """ 测试除法类 """ cases_list = do_excel.get_cases() @classmethod def setUpClass(cls): """ 重写父类的类方法,全部实例方法(用例)执行完只会被调用1次 :return: """ # cls.file_name = 'test_result.txt' # cls.file_name = do_config('file path', 'log_path') # print('打开【{}】文件'.format(cls.file_name))
# 打开文件 # file = open('test_result_3.txt', mode='w', encoding='utf8') # 创建一个运行器对象,将测试结果存放到文件中 # runner = unittest.TextTestRunner(stream=file, descriptions='测试报告', verbosity=2) # runner.run(suite) # 关闭文件 # file.close() # 上下文管理器,无需关闭文件 # with open('test_result_3.txt', mode='w', encoding='utf8') as file: # runner = unittest.TextTestRunner(stream=file, descriptions='测试报告', verbosity=2) # runner.run(suite) # 美化报告 # runner = HTMLTestRunner(output='report', report_name='测试报告', report_title='测试报告标题', combine_reports=True) # runner.run(suite) # 进一步美化报告 report_path = do_config('file path', 'report_path') report_path_full = report_path + '_' + datetime.strftime( datetime.now(), '%Y%m%d%H%M%S') + '.html' with open(report_path_full, mode='wb') as file: runner = HTMLTestRunnerNew.HTMLTestRunner(stream=file, title='第一份测试报告', verbosity=2, description='测试两数相除/相减用例', tester='dranson') runner.run(suite) # 执行结果中,一个"."代表执行成功一条用例,一个"F"代表执行失败一条用例
import unittest import inspect from Class_16_Homework.homework_0520.ddt import ddt, data # 导入ddt,ddt和data必须同时导入 # 导入数学计算的测试类 from Class_16_Homework.homework_0520.math_cal import MathCalculate # 导入Excel类 from Class_16_Homework.homework_0520.excel_class import HandleExcel # 导入配置文件类 from Class_16_Homework.homework_0520.config_class import do_config # 导入日志类 from Class_16_Homework.homework_0520.log_class import do_log do_excel = HandleExcel(do_config('file path', 'cases_path'), 'minus') @ddt # 在类的上一行加 class TestMinus(unittest.TestCase): """ 测试减法类 """ cases_list = do_excel.get_cases() @classmethod def setUpClass(cls): """ 重写父类的类方法,全部实例方法(用例)执行完只会被调用1次 :return: """ # cls.file_name = 'test_result.txt' # cls.file_name = do_config('file path', 'log_path') # print('打开【{}】文件'.format(cls.file_name))