def test_login(self, case): login_url = do_yaml.read("api", "host") + case["url"] # 构建完成的接口地址 login_params = HandleParam.do_param( case["data"]) # 取出需要传递的参数(参数化之后的需要替换为实际的) login_res = self.do_request.send( login_url, data=login_params) # 发起请求,并把请求结果转成json,赋值给register_res login_res_dict = login_res.json() print(login_res) case_title = case["title"] # 测试用例项名称 success_msg = do_yaml.read("msg", "success_result") fail_msg = do_yaml.read("msg", "fail_result") try: assert json.loads( case["expected"], encoding="utf8").get("code") == login_res_dict["code"] assert json.loads( case["expected"], encoding="utf8").get("msg") == login_res_dict["msg"] except AssertionError as e: # # 执行失败,把结果写入excel # self.excel.write_data(row=case["case_id"] + 1, column=do_yaml.read("excel", "result_col"), value=fail_msg) # do_log.error(f"{case_title}的执行结果为:{fail_msg}\n具体异常为{e}") raise e else: # 执行成功,也把结果写入excel self.excel.write_data(row=case["case_id"] + 1, column=do_yaml.read("excel", "result_col"), value=success_msg) finally: # 把响应报文写入excel self.excel.write_data(row=case["case_id"] + 1, column=do_yaml.read("excel", "actual_col"), value=login_res.text)
def __init__(self): self.conn = pymysql.connect(host=do_yaml.read("mysql", "host"), user=do_yaml.read("mysql", "user"), password=do_yaml.read("mysql", "password"), db=do_yaml.read("mysql", "db"), port=do_yaml.read("mysql", "port"), charset="utf8", cursorclass=pymysql.cursors.DictCursor) self.cursor = self.conn.cursor()
def __init__(self, sheetname, filename=None): if filename is None: self.filename = os.path.join(DATAS_DIR, do_yaml.read("excel", "cases_path")) else: self.filename = filename self.sheetname = sheetname
def create_new_user(reg_name, pwd="12345678", user_type=1): """ 创建一个用户 :param reg_name: 用户名 :param pwd: 密码,默认为123456 :param user_type: 用户类型 默认为1(0:管理员,1:普通会员) :return: 存储一个用户信息, 嵌套字典的字典(以用户名为key, 以用户信息所在字典为value) """ do_mysql = HandleMysql() # 创建数据库对象 do_request = HandleRequests() # 创建request对象 # 构建请求参数,向注册接口发起请求 do_request.add_headers(do_yaml.read("api", "version")) # 添加公共请求头 url = do_yaml.read("api", "host") + '/member/register' # 构建请求url sql = do_yaml.read("mysql", "select_userid_sql") # 查询用户id的sql语句 while True: mobile_phone = do_mysql.create_not_existed_mobile() # 生成一个未注册的手机号 # 构建请求体 data = { "mobile_phone": mobile_phone, "pwd": pwd, "reg_name": reg_name, "type": user_type } do_request.sent(url=url, data=data) # 发起请求 # 从数据库查询注册后的用户的id result = do_mysql.run(sql, args=(mobile_phone, )) # 判断是否查询到,如果查询到就跳出循环,查询不到继续循环 if result: user_id = result["id"] break # 构建用户数据字典 user_dic = { reg_name: { "id": user_id, "mobile_phone": mobile_phone, "reg_name": reg_name, "pwd": pwd, "type": user_type } } # 把用户数据返回 do_mysql.close() do_request.close() return user_dic # 把构建的字典返回
def setup_class(self): self.do_request = HandleRequests() # 在所有用例执行之前,创建HandlerRequest对象 self.do_request.add_headers(do_yaml.read("api", "version")) # 添加公共请求头,接口版本号
def is_existed_sqlexec(self, wherestr): sql = do_yaml.read("mysql", "select_demo_sql") if self.run(sql, args=[wherestr]): return True else: return False
# @Software: PyCharm import unittest from datetime import datetime import os # suite = unittest.TestSuite() from test_interface.libs.HTMLTestRunnerNew import HTMLTestRunner from test_interface.scripts.handle_path import USER_ACCOUNTS_FILE_PATH, CASES_DIR, REPORTS_DIR from test_interface.scripts.handle_user import write_user_info from test_interface.scripts.handle_yaml import do_yaml if not os.path.exists(USER_ACCOUNTS_FILE_PATH): write_user_info() suite = unittest.defaultTestLoader.discover(CASES_DIR) # loader = unittest.TestLoader() # if not os.path.isfile(os.path.join(CONFIGS_DIR,"rules_user.yaml")): # RegisterUser.test_register_three_users() # suite.addTest(loader.loadTestsFromModule(test_login_api)) result_full_path = do_yaml.read("report", "name") + "_" + \ datetime.strftime(datetime.now(), "%Y%m%d%H%M%S") + ".html" result_full_path = os.path.join(REPORTS_DIR, result_full_path) with open(result_full_path, "wb") as fb: runner = HTMLTestRunner(stream=fb, title=do_yaml.read('report', 'title'), description=do_yaml.read('report', 'description'), tester=do_yaml.read('report', 'tester')) runner.run(suite)
def is_existed_mobile(self, mobile): sql = do_yaml.read("mysql", "select_user_sql") if self.run(sql, args=[mobile]): return True else: return False