def test_add(self, data_namedtuple): case_id = data_namedtuple.case_id + 1 title = data_namedtuple.title new_data = HandleParameter.add_parameter(data_namedtuple.data) response = TestAdd.do_request(method=data_namedtuple.method, url=do_config("api", "prefix_url") + data_namedtuple.url, data=new_data) run_success_msg = do_config("msg", "success_result") run_fail_msg = do_config("msg", "fail_result") try: self.assertEqual(data_namedtuple.expected, response.text, msg="测试【{}】失败".format(title)) except AssertionError as e: do_log.error("{},测试结果为:{}\n,具体异常信息为{}".format( title, run_fail_msg, e)) self.parser_excel.write_result_to_excel(row=case_id, actual=response.text, result=run_fail_msg) raise e else: do_log.info("{},测试结果为:{}".format(title, run_success_msg)) self.parser_excel.write_result_to_excel(row=case_id, actual=response.text, result=run_success_msg)
def setUpClass(cls) -> None: cls.filename = os.path.join(LOGS_DIR, do_config("file_path", "log_path")) cls.file = open(cls.filename, mode="a", encoding="utf-8") do_log.info("{}".format("开始执行投资功能用例")) cls.do_request = HttpRequest() cls.do_mysql = HandleMysql()
def write_result_to_excel(self, row, actual, result): """ 将测试结果写入到excel中 :param row: :param actual: :param result: :return: """ other_wb = load_workbook(self.filename) other_ws = other_wb[self.sheet_name] if self.sheet_name is not None else other_wb.active if isinstance(row, int) and 2 <= row <= self.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) else: print("传递的行数有误!")
def setUpClass(cls) -> None: """ 前置处理操作 :return: """ cls.filename = LOGS_DIR + r"\\" + do_config("file_path", "log_path") cls.file = open(cls.filename, mode="a", encoding="utf-8") do_log.info("{}".format("开始执行登录功能用例")) cls.do_request = HttpRequest()
def test_recharge(self, data_namedtuple): run_success_msg = do_config("msg", "success_result") run_fail_msg = do_config("msg", "fail_result") case_id = data_namedtuple.case_id+1 title = data_namedtuple.title new_data = json.loads(HandleParameter.recharge_parameter(data_namedtuple.data)) check_sql = data_namedtuple.check_sql if check_sql: leaveAmount_before_recharge = TestRecharge.do_mysql(check_sql, args=(new_data["mobilephone"], )) response = TestRecharge.do_request(method=data_namedtuple.method, url=do_config("api", "prefix_url")+data_namedtuple.url, data=new_data) actual = json.loads(response.text) actual["data"] = None try: self.assertEqual(200, response.status_code, msg="测试【{}】时,请求失败,状态码为【{}】". format(title, response.status_code)) except AssertionError as e: do_log.error("具体异常信息为:".format(e)) raise e try: self.assertEqual(json.loads(data_namedtuple.expected), actual, msg="测试【{}】失败".format(title)) if check_sql: leaveAmount_after_recharge = TestRecharge.do_mysql(check_sql, args=(new_data["mobilephone"],)) actual_amount = leaveAmount_after_recharge["LeaveAmount"] - leaveAmount_before_recharge["LeaveAmount"] self.assertEqual(float(new_data["amount"]), round(float(actual_amount)), msg="充值金额有误") except AssertionError as e: do_log.error("{},测试结果为:{}\n,具体异常信息为{}".format(title, run_fail_msg, e)) self.parser_excel.write_result_to_excel(row=case_id, actual=response.text, result=run_fail_msg) raise e else: do_log.info("{},测试结果为:{}".format(title, run_success_msg)) self.parser_excel.write_result_to_excel(row=case_id, actual=response.text, result=run_success_msg)
def test_invest(self, data_namedtuple): case_id = data_namedtuple.case_id + 1 title = data_namedtuple.title new_data = HandleParameter.invest_parameter(data_namedtuple.data) response = TestInvest.do_request(method=data_namedtuple.method, url=do_config("api", "prefix_url") + data_namedtuple.url, data=new_data) run_success_msg = do_config("msg", "success_result") run_fail_msg = do_config("msg", "fail_result") """反射""" if response.json().get("msg") == "加标成功": check_sql = data_namedtuple.check_sql if check_sql: check_sql = HandleParameter.invest_parameter(check_sql) sql_data = TestInvest.do_mysql(sql=check_sql) HandleParameter.loan_id = sql_data.get("Id") # setattr(HandleParameter, "loan_id", loan_id) try: self.assertEqual(data_namedtuple.expected, response.text, msg="测试【{}】失败".format(title)) except AssertionError as e: do_log.error("{},测试结果为:{}\n,具体异常信息为{}".format( title, run_fail_msg, e)) self.parser_excel.write_result_to_excel(row=case_id, actual=response.text, result=run_fail_msg) raise e else: do_log.info("{},测试结果为:{}".format(title, run_success_msg)) print("{}: {}".format(type(response.text), response.text)) self.parser_excel.write_result_to_excel(row=case_id, actual=response.text, result=run_success_msg)
def __init__(self): # 1.建立连接 self.conn = pymysql.connect(host=do_config("mysql", "host"), port=do_config("mysql", "port"), user=do_config("mysql", "user"), password=str(do_config("mysql", "password")), # 注意是字符串 db=do_config("mysql", "db"), charset=do_config("mysql", "charset"), # utf-8会报错 cursorclass=pymysql.cursors.DictCursor) # 2.获取游标 self.cursor = self.conn.cursor()
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( os.path.join(LOGS_DIR, do_config("file_path", "log_path")), maxBytes=3 * 1024 * do_config("log", "maxBytes"), backupCount=do_config("log", "backupCount"), encoding="utf-8") console_handle.setLevel("ERROR") file_handle.setLevel("INFO") simple_formatter = logging.Formatter( do_config("log", "simple_formatter")) common_formatter = logging.Formatter( do_config("log", "common_formatter")) console_handle.setFormatter(simple_formatter) file_handle.setFormatter(common_formatter) self.case_logger.addHandler(console_handle) self.case_logger.addHandler(file_handle)
@Software :PyCharm ******************************** """ import os import unittest from datetime import datetime from MyFinanceAPI.libs.HTMLTestRunnerNew import HTMLTestRunner from MyFinanceAPI.scripts.parser_config import do_config from MyFinanceAPI.scripts.constants import REPORTS_DIR from MyFinanceAPI.scripts.constants import CASES_DIR from MyFinanceAPI.scripts.constants import USER_CONFIGS_FILE_PATH from MyFinanceAPI.scripts.handle_user import generate_user_config if not os.path.exists(USER_CONFIGS_FILE_PATH): generate_user_config() one_suite = unittest.defaultTestLoader.discover(CASES_DIR) report_name = os.path.join(REPORTS_DIR, do_config("report", "report_name")) # full_html_report_name = report_name+"_"+datetime.strftime(datetime.now(), "%Y%m%d%H%M%S")+".html" full_html_report_name = report_name + "_" + datetime.strftime( datetime.now(), "%Y%m%d%H%M%S") + ".html" with open(full_html_report_name, mode="wb") as file: one_runner = HTMLTestRunner(stream=file, verbosity=do_config("report", "verbosity"), title=do_config("report", "title"), description=do_config("report", "description"), tester=do_config("report", "tester")) one_runner.run(one_suite)