def run(self): suite = unittest.defaultTestLoader.discover(self.cases_dir) runner = HTMLTestRunner( stream=open(self.report_name, 'wb'), verbosity=2, title=configuration.getConfig('report', 'title'), description=configuration.getConfig('report', 'description'), tester=configuration.getConfig('report', 'tester')) runner.run(suite)
def __init__(self): self.conn = pymysql.connect(host=configuration.getConfig('mysql', 'host'), user=configuration.getConfig('mysql', 'user'), password=configuration.getConfig('mysql', 'password'), db=configuration.getConfig('mysql', 'db'), port=configuration.getConfig('mysql', 'port'), charset='utf8', cursorclass=pymysql.cursors.DictCursor # 让返回结果为字典类型 ) self.cursor = self.conn.cursor()
def run(self): if self.cases is None: suite = unittest.defaultTestLoader.discover(self.cases_dir) else: sys.path.insert(0, self.cases_dir) suite = unittest.TestLoader().loadTestsFromNames(self.cases) runner = HTMLTestRunner(stream=open(self.report_name, 'wb'), verbosity=2, title=configuration.getConfig('report', 'title'), description=configuration.getConfig('report', 'description'), tester=configuration.getConfig('report', 'tester')) runner.run(suite)
def __init__(self, sheetname=None, filename=None): if filename is None: self.filename = os.path.join( EXCEL_FILE_PATH, configuration.getConfig('excel', 'excelname')) else: self.filename = filename self.sheetname = sheetname
def creat_logger(): # 创建一个日志收集器 my_log = logging.getLogger( configuration.getConfig('log', 'logger_name')) # 设置日志收集器的等级 my_log.setLevel(configuration.getConfig('log', 'log_level')) # 设置日志输出格式 formatter = logging.Formatter( configuration.getConfig('log', 'formatter')) # 日志的输出 # 创建一个输出到控制台的数据流 sh = logging.StreamHandler() sh.setLevel(configuration.getConfig('log', 'output_level')) # 设置输出到控制台的日志格式 sh.setFormatter(formatter) # 将输出渠道加入到日志收集器中 my_log.addHandler(sh) # 创建一个输出到文件的日志数据流 fh = logging.FileHandler(os.path.join( LOGS_FILE_PATH, configuration.getConfig('log', 'filelog_name')), encoding='utf-8') fh.setLevel(configuration.getConfig('log', 'file_level')) # 设置输出到文件的日志格式 fh.setFormatter(formatter) my_log.addHandler(fh) return my_log
def __init__(self, cases_dir=None, report_name=None): super().__init__() if cases_dir is not None: self.cases_dir = cases_dir else: self.cases_dir = CASES_DIR if report_name is not None: r_name = report_name + '.html' self.report_name = os.path.join(REPORTS_FILE_PATH, r_name) else: r_name = configuration.getConfig('report', 'name') + '_' + \ datetime.strftime(datetime.now(), '%Y%m%d%H%M%S') + '.html' self.report_name = os.path.join(REPORTS_FILE_PATH, r_name)
def test_token(self, case): # 1.将测试用例中的参数参数化 # new_data = HandleParameterize.do_param(case.data) # 2.拼接url url = configuration.getConfig('api', 'prefix') + case.url # 3.向服务器发起请求 new_request = self.do_request.send(url=url, method=case.method, data=case.data, is_json=False) # 4.将响应报文里的数据转化为字典格式 # actual_result = new_request.json() actual_result = new_request.status_code # 获取预期的结果,将预期结果转化为字典 # expected = json.loads(case.expected, encoding='utf-8') # expected_code = expected['code'] # expected_msg = expected['msg'] expected_code = case.expected # 获取title title = case.title # 获取执行成功的提示信息 success_msg = configuration.getConfig('msg', 'success_result') # 获取执行失败的提示信息 failed_msg = configuration.getConfig('msg', 'fail_result') # 获取行号 row = case.case_id + 1 # 5.断言,预期结果与实际结果做对比 try: # self.assertEqual(expected_code, actual_result['code'], msg=title) # self.assertEqual(expected_msg, actual_result['msg'], msg=title) self.assertEqual(expected_code, actual_result, msg=title) except AssertionError as e: # 将实际值写入到actual_rol列 self.do_excel.write_to_file(row=row, column=configuration.getConfig( 'excel', 'actuall_col'), value=new_request.text) # 将实际值写入到result_col列 self.do_excel.write_to_file(row=row, column=configuration.getConfig( 'excel', 'result_col'), value=failed_msg) log.error(f'{title},执行的结果为:{failed_msg}\n具体异常为:{e}') raise e else: # 将实际值写入到actual_rol列 self.do_excel.write_to_file(row=row, column=configuration.getConfig( 'excel', 'actuall_col'), value=actual_result) # 将实际结论写入到result_col列 self.do_excel.write_to_file(row=row, column=configuration.getConfig( 'excel', 'result_col'), value=success_msg) log.info(f'{title},执行的结果为:{success_msg}')
def setUpClass(cls): cls.do_request = HandleRequest() cls.do_request.add_headers(configuration.getConfig('api', 'version'))