def test_negative_multi(self, data_namedtuple): """ 1.两个负数相乘 :return: """ # print("\nrunning test method:{}".format(inspect.stack()[0][3])) do_log.info("\nrunning test method:{}".format(inspect.stack()[0][3])) # 获取两个负数相乘的结果 case_id = data_namedtuple.case_id msg = data_namedtuple.title l_data = data_namedtuple.l_data r_data = data_namedtuple.r_data except_result = data_namedtuple.expected real_result = MathOperation( l_data, r_data).multiply() # 类+() 是对象 用对象调用类中的实例multiply() # 将实际结果写入Excel # run_success_msg = TestMulti("msg","success_result") run_success_msg = do_config("msg", "success_result") # run_fail_msg = TestMulti("msg", "fail_result") run_fail_msg = do_config("msg", "fail_result") try: self.assertEqual(except_result, real_result, msg="测试{}失败".format(msg)) except AssertionError as e: # print("这里需要使用日志器来记录日志") # print("具体异常为:{}".format(e)) do_log.error("具体异常为:{}".format(e)) # ws.cell(row=case_id+1,column=7,value="Fail") # self.test_obj.write_result(row=case_id+1,actual=real_result,result="Fail") # self.test_obj.write_result(row=case_id+1,actual=real_result,result=self.config("msg","fail_result")) do_excel.write_result(row=case_id + 1, actual=real_result, result=do_config("msg", "fail_result")) # self.assertRaises(TypeError) # assertRaises可以直接断言异常 # raise关键字是将某个异常主动抛出 raise e else: # ws.cell(row=case_id+1,column=7,value="Pass") # self.test_obj.write_result(row=case_id+1,actual=real_result,result="Pass") do_excel.write_result(row=case_id + 1, actual=real_result, result=do_config("msg", "success_result"))
def write_result(self, row, actual, result): """ 将实际值与测试用例执行的结果保存到Excel中 :param row:保存到哪一行 :param actual:实际结果 :param result:测试用例执行结果'Pass''Fail' :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=self.config("excel","actual_col"),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=HandleExcel.config("excel","result_col"),value=result) self.ws.cell(row=row, column=do_config("excel", "result_col"), value=result) self.wb.save(self.filename)
def __init__(self): self.case_logger = logging.getLogger(do_config("log", "logger_name")) self.case_logger.setLevel(do_config("log", "logger_level")) console_handle = logging.StreamHandler() file_handle = RotatingFileHandler( filename=do_config("log", "log_filename"), maxBytes=do_config("log", "max_byte"), backupCount=do_config("log", "backcount"), encoding="utf-8") console_handle.setLevel(do_config("log", "console_level")) file_handle.setLevel(do_config("log", "file_level")) simple_log = logging.Formatter(do_config("log", "simple_log")) verbose_log = logging.Formatter(do_config("log", "verbose_log")) console_handle.setFormatter(simple_log) file_handle.setFormatter(verbose_log) self.case_logger.addHandler(console_handle) self.case_logger.addHandler(file_handle)
# # 将测试结果存放到文件中 # one_runner = unittest.TextTestRunner(stream=save_to_file,descriptions="测试结果集",verbosity=1) # verbosity对应输出日志报告详细程度2最详细,0普通 # one_runner.run(one_suite) # # 关闭文件 # save_to_file.close() # # 打开关闭文件太麻烦的话可以用with as 上下文管理器,这样就不用担心文件是否关闭 # with open("test_results.txt",mode="w",encoding="utf-8") as save_to_file: # one_runner = unittest.TextTestRunner(stream=save_to_file, descriptions="测试结果集", verbosity=1) # one_runner.run(one_suite) # 进一步美化输出日志报告,安装第三方包,file - settings - project interpreter - + - html-testRunner - install Package 或者用pip # pip install html-testRunner # # output输出生成指定名称的文件夹,会针对单独的测试类来生成HTML的报表,打开要放在html文件上右击选择Open in Browser 选择浏览器 # one_runner = HTMLTestRunner(output="reports",report_name='测试结果集',report_title="标题,html会乱码",combine_reports=True) # one_runner.run(one_suite) # 执行用例 test_results_202012151043.html 需要这种格式 report_html_name = do_config("file path", "report_html_name") report_html_name_full = report_html_name + "_" + datetime.strftime( datetime.now(), "%Y%m%d%H%M%S") + ".html" with open(report_html_name_full, mode="wb") as save_to_file: one_runner = HTMLTestRunnerNew.HTMLTestRunner(stream=save_to_file, title="测试结果集", verbosity=1, description="测试两数相乘用例", tester="静宝") one_runner.run(one_suite)
import unittest # 修改ddt源码,把class13中的很长的字符拼接test_negative_multi_2_test_case_id_2__title__负数与正数相乘___l_data__3__r_data_4__expected_12__actual__12__result__Fail__改短 from Python_interface.ddt import ddt,data import inspect from collections import namedtuple from openpyxl import load_workbook from pythonbase_class_1.Class_9_1_MathOperation import MathOperation # from Python_interface.Class_5_excel_encapsulation import HandleExcel # from Python_interface.Class_8_ParserConfigFile__call__ import Handleconfig # from Python_interface.Class_11_Rewrite_excel_encapsulation import do_excel from Python_interface.Class_11_Rewrite_excel_encapsulation import HandleExcel # 因为运算加法在第二个sheet里,而do_excel对象指定的是第一个sheet,所有我们直接导类,不导对象 from Python_interface.Class_8_ParserConfigFile__call__ import do_config from Python_interface.Class_10_Log_encapsulation import do_log do_excel = HandleExcel(do_config("file path","test_path"),"add") # 重新定义do_excel @ddt class TestAdd(unittest.TestCase): # unittest规则:每一条用例,使用实例方法来测试,并且实例方法吗要以test_开头 """ 测试两数相加 """ # config = Handleconfig() # # test_obj = HandleExcel("test.xlsx") # test_obj = HandleExcel(config("file path","test_path")) # tests = test_obj.get_tests() tests = do_excel.get_tests() @classmethod def setUpClass(cls): """
def write_result(self, row, actual, result): """ 将实际值与测试用例执行的结果保存到Excel中 :param row:保存到哪一行 :param actual:实际结果 :param result:测试用例执行结果'Pass''Fail' :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=self.config("excel","actual_col"),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=HandleExcel.config("excel","result_col"),value=result) self.ws.cell(row=row, column=do_config("excel", "result_col"), value=result) self.wb.save(self.filename) do_excel = HandleExcel(filename=do_config("file path", "test_path")) if __name__ == '__main__': file_name = "test.xlsx" one_excel = HandleExcel(filename=file_name) test = one_excel.get_tests() print(test)
# time : 2020/12/14 0014 21:10 --------------------------------------------------- """ import unittest # 修改ddt源码,把class13中的很长的字符拼接test_negative_multi_2_test_case_id_2__title__负数与正数相乘___l_data__3__r_data_4__expected_12__actual__12__result__Fail__改短 from Python_interface.ddt import ddt, data import inspect from pythonbase_class_1.Class_9_1_MathOperation import MathOperation # from Python_interface.Class_5_excel_encapsulation import HandleExcel # from Python_interface.Class_8_ParserConfigFile__call__ import Handleconfig # from Python_interface.Class_11_Rewrite_excel_encapsulation import do_excel # 因为运算加法在第二个sheet里,而do_excel对象指定的是第一个sheet,所有我们直接导类,不导对象 from Python_interface.Class_11_Rewrite_excel_encapsulation import HandleExcel from Python_interface.Class_8_ParserConfigFile__call__ import do_config from Python_interface.Class_10_Log_encapsulation import do_log do_excel = HandleExcel(do_config("file path", "test_path"), "multi") @ddt class TestMulti(unittest.TestCase ): # unittest规则:每一条用例,使用实例方法来测试,并且实例方法吗要以test_开头 """ 测试两数相乘 """ # config = Handleconfig() # # test_obj = HandleExcel("test.xlsx") # test_obj = HandleExcel(config("file path","test_path")) # tests = test_obj.get_tests() tests = do_excel.get_tests() @classmethod