def test_projects(self, test_info): data_info = test_info["data"] # 替换data中的#name# if "#name#" in data_info: name = random_handler.RandomHandler().project_name() data_info = data_info.replace("#name#", name[0]) # 替换data中的#projectname1# if "#projectname1#" in data_info: name = random_handler.RandomHandler().project_name() data_info = data_info.replace("#projectname1#", name[1]) # 替换data中的#projectname2# if "#projectname2#" in data_info: name = random_handler.RandomHandler().project_name() data_info = data_info.replace("#projectname2#", name[2]) # 替换data中的#pass_name# if "#pass_name#" in data_info: name = MySqlHandler().get_db( "select name from test.tb_projects where id= 2 limit 1;" )["name"] data_info = data_info.replace("#pass_name#", name) # 替换headers中的#token# if "#token#" in test_info["headers"]: test_info["headers"] = test_info["headers"].replace( "#token#", self.token) # 发送请求 res = request_handler.request_handler( url=yaml["url"] + test_info["url"], method=test_info["method"], headers=json.loads(test_info["headers"]), json=json.loads(data_info)).text # 替换预期结果中的#id# if "#id#" in test_info["excepted"]: id = MySqlHandler().get_db( "select id from test.tb_projects where name='{}' limit 1;". format(json.loads(data_info)["name"])) test_info["excepted"] = test_info["excepted"].replace( "#id#", str(id)) # 替换预期结果中的#name# if "#name#" in test_info["excepted"]: test_info["excepted"] = test_info["excepted"].replace( "#name#", json.loads(data_info)["name"]) # 断言 try: for k, v in json.loads(test_info["excepted"]).items(): # 断言预期结果和实际结果是否一样 self.assertEqual(v, json.loads(res)[k]) self.result = "pass" logger.info('第{}条用例通过'.format(test_info["case_id"])) except AssertionError as e: self.result = "fail" logger.error('第{}条用例失败,失败原因{}'.format(test_info["case_id"], e)) raise e finally: MiddleHandler.case_path.open_excel() MiddleHandler.case_path.write_excel(sheet_name="projects", row=test_info["case_id"] + 1, colucm=9, information=self.result)
def login(self): username = self.register() data = { "username": username, "password": "******", } res = request_handler.request_handler(url=MiddleHandler.yaml["url"] + "/user/login/", method="post", json=data).text username = json.loads(res)["username"] token_data = json.loads(res)["token"] token = " ".join(["JWT", token_data]) return [username, token]
def register(self): username = (random_handler.RandomHandler().username())[0] email = random_handler.RandomHandler().email() data = { "username": username, "email": email, "password": "******", "password_confirm": "123456" } res = request_handler.request_handler(url=MiddleHandler.yaml["url"] + "/user/register/", method="post", json=data).text return json.loads(res)["username"]
def test_login(self, test_info): data_info = test_info["data"] # 替换data中的#username# if "#username#" in data_info: data_info = data_info.replace("#username#", self.username) # 替换data中的#password# if "#password#" in data_info: data_info = data_info.replace("#password#", yaml["password"]) # 发送请求 res = request_handler.request_handler(url=yaml["url"] + test_info["url"], method=test_info["method"], json=json.loads(data_info)).text # 如果预期结果中有#id#,查询数据库中的id并替换 if "#id#" in test_info["excepted"]: db = MySqlHandler() id = db.get_db( "select id from test.auth_user where username='******';".format( json.loads(data_info)["username"]))["id"] test_info["excepted"] = test_info["excepted"].replace( "#id#", str(id)) # 如果预期结果中有#username#,替换data中的username if "#username#" in test_info["excepted"]: test_info["excepted"] = test_info["excepted"].replace( "#username#", json.loads(data_info)["username"]) # 断言 try: for k, v in json.loads(test_info["excepted"]).items(): # 断言预期结果和实际结果是否一样 self.assertEqual(v, json.loads(res)[k]) self.result = "pass" logger.info('第{}条用例通过'.format(test_info["case_id"])) except AssertionError as e: self.result = "fail" logger.error('第{}条用例失败,失败原因{}'.format(test_info["case_id"], e)) raise e finally: MiddleHandler.case_path.open_excel() MiddleHandler.case_path.write_excel(sheet_name="login", row=test_info["case_id"] + 1, colucm=8, information=self.result)
def test_username(self, test_info): data_info = test_info["url"] # 替换用例url数据中的#pass_email# if "#pass_email#" in data_info: db = MySqlHandler() pass_email = db.get_db( "select email from test.auth_user where id=1;")["email"] data_info = data_info.replace("#pass_email#", pass_email) # 替换用例url数据中的#email# if "#email#" in data_info: email = random_handler.RandomHandler().email() data_info = data_info.replace("#email#", email) res = request_handler.request_handler( url=yaml["url"] + data_info, method=test_info["method"], ).text # 如果预期结果中有#username#,替换生成的username if "#email#" in test_info["excepted"]: test_info["excepted"] = test_info["excepted"].replace( "#email#", (data_info.split("/"))[2]) # 如果预期结果中有#pass_username#,替换生成的pass_username if "#pass_email#" in test_info["excepted"]: test_info["excepted"] = test_info["excepted"].replace( "#pass_email#", (data_info.split("/"))[2]) # 断言 try: for k, v in json.loads(test_info["excepted"]).items(): # 断言预期结果和实际结果是否一样 self.assertEqual(v, json.loads(res)[k]) self.result = "pass" logger.info('第{}条用例通过'.format(test_info["case_id"])) except AssertionError as e: self.result = "fail" logger.error('第{}条用例失败,失败原因{}'.format(test_info["case_id"], e)) raise e finally: MiddleHandler.case_path.open_excel() MiddleHandler.case_path.write_excel(sheet_name="username", row=test_info["case_id"] + 1, colucm=8, information=self.result)
def projects(self): name = (random_handler.RandomHandler().project_name())[0] data = { "name": name, "leader": "天空", "tester": "sky", "programmer": "haha", "publish_app": "天空是富婆", "desc": "当我的笑灿烂像阳光" } res = request_handler.request_handler( url=MiddleHandler.yaml["url"] + "/projects/", method="post", headers={ "Authorization": self.login()[1] }, json=data).text project_id = json.loads(res)["id"] project_name = json.loads(res)["name"] return [project_id, project_name]
def interfaces(self): interfaces_name = (random_handler.RandomHandler().interfaces_name())[0] project = self.projects() project_id = project[0] project_name = project[1] interfaces_data = { "name": interfaces_name, "tester": "天空", "project_id": project_id, "desc": "这是一个接口" } interfaces_res = request_handler.request_handler( url=MiddleHandler.yaml["url"] + "/interfaces/", method="post", headers={ "Authorization": self.login()[1] }, json=interfaces_data).text interfaces_id = json.loads(interfaces_res)["id"] interfaces_name = json.loads(interfaces_res)["name"] return [interfaces_id, interfaces_name, project_id, project_name]
def test_register(self, test_info): # 替换用例中data的#username# data = test_info["data"] if "#username#" in data: username = random_handler.RandomHandler().username() data = data.replace("#username#", username[0]) # 替换用例中data的#username1# if "#username1#" in data: username = random_handler.RandomHandler().username() data = data.replace("#username1#", username[1]) # 替换用例中data的#username1# if "#username2#" in data: username = random_handler.RandomHandler().username() data = data.replace("#username2#", username[2]) # 替换用例data中的#pass_username# # 从数据库中提取一个已存在的用户名 if "#pass_username#" in data: db = MySqlHandler() pass_username = db.get_db( "select username from test.auth_user where id=1;")["username"] data = data.replace("#pass_username#", pass_username) # 替换用例中data的#email# if "#email#" in data: email = random_handler.RandomHandler().email() data = data.replace("#email#", email) # 替换用例data中的#pass_email# # 从数据库中提取一个已存在的用email if "#pass_email#" in data: db = MySqlHandler() pass_email = db.get_db( "select email from test.auth_user where id=1;")["email"] data = data.replace("#pass_email#", pass_email) res = request_handler.request_handler(url=yaml["url"] + test_info["url"], method=test_info["method"], json=json.loads(data)).text # 如果预期结果中有#username#,替换生成的username if "#username#" in test_info["excepted"]: test_info["excepted"] = test_info["excepted"].replace( "#username#", json.loads(data)["username"]) # 如果预期结果中有#id#,查询数据库,替换#id# if "#id#" in test_info["excepted"]: db = MySqlHandler() id = db.get_db( "select id from test.auth_user where username='******';". format(json.loads(data)["username"]))["id"] test_info["excepted"] = test_info["excepted"].replace( "#id#", str(id)) # 断言 try: for k, v in json.loads(test_info["excepted"]).items(): # 断言预期结果和实际结果是否一样 self.assertEqual(v, json.loads(res)[k]) self.result = "pass" logger.info('第{}条用例通过'.format(test_info["case_id"])) except AssertionError as e: self.result = "fail" logger.error('第{}条用例失败,失败原因{}'.format(test_info["case_id"], e)) raise e finally: MiddleHandler.case_path.open_excel() MiddleHandler.case_path.write_excel(sheet_name="register", row=test_info["case_id"] + 1, colucm=8, information=self.result)