def run_api(url, method, params_type, header=None, cookie=None, params=None): """ 发送api请求 """ request = Request() if str(method).lower() == "get": if str(params_type).lower() == "query_string": r = request.get(url, headers=header, data=params, cookies=cookie) else: r = request.get(url, headers=header, cookies=cookie) elif str(method).lower() == "post": if str(params_type).lower() == "form_data": r = request.post(url, data=params, headers=header, cookies=cookie) elif str(params_type).lower() == "json": r = request.post(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "put": if str(params_type).lower() == "form_data": r = request.put(url, data=params, headers=header, cookies=cookie) elif str(params_type).lower() == "json": r = request.post(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "delete": if str(params_type).lower() == "form_data": r = request.post(url, data=params, headers=header, cookies=cookie) elif str(params_type).lower() == "json": r = request.post(url, json=params, headers=header, cookies=cookie) else: log.error("错误请求methods:", method) return r
def run_api(self, url, method, url_params, reference, params=None, header=None, cookie=None): """ 发送请求api :param url: :param method: :param params: :param header: :param cookie: :return: """ request = Request() # params 转义json if len(str(params).strip()) is not 0: params = json.loads(params) if str(method).lower() == "get" and str(reference).lower() == "b": res = request.get(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "get" and str(reference).lower() == "u": res = request.get(url_params, headers=header, cookies=cookie) elif str(method).lower() == "post" and str(reference).lower() == "b": res = request.post(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "post" and str(reference).lower() == "u": res = request.post(url_params, headers=header, cookies=cookie) else: log.error("错误请求method: %s" % method) return res
def run_api(url, method, params=None, body=None, header=None, cookie=None): request = Request() if str(method).lower() == "get": res = request.get(url, params=params, headers=header, cookies=cookie) elif str(method).lower() == "post": res = request.post(url, params=params, json=body, headers=header, cookies=cookie) elif str(method).lower() == "put": res = request.put(url, json=body, params=params, headers=header, cookies=cookie) elif str(method).lower() == "delete": res = request.delete(url, params=params, headers=header, cookies=cookie) else: log.error("请求方法 method 错误:method写成了 %s" % method) raise return res
def test_run(self, case): # 3. 重构函数内容 data_key = ExcelConfig.DataConfig # run_list第一个用例,根据key获取values url = ConfigYaml().get_conf_url() + case[data_key.url] # print(url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] status_code = case[data_key.status_code] db_verify = case[data_key.db_verify] # 2).根据url发送接口请求 request = Request() # params 获取的是字符类型 需要转义成json # 验证params有没有内容 if len(str(params).strip()) is not 0: params = json.loads(params) # method: post / get if str(method).lower() == "get": # 2.增加headers res = request.get(url, json=params) elif str(method).lower() == "post": res = request.post(url, json=params) else: log.error("错误请求method:%s" % method) print(res)
def test_login(): # 3、定义测试数据 conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/authorizations/" # url = "http://211.103.136.242:8064/authorizations/" data = {"username": "******", "password": "******"} # 4、发送POST请求 # r = requests.post(url,json=data) # r = requests_post(url,json=data) request = Request() r = request.post(url, json=data) # 5、输出结果 # print(r.json()) print(r) # 返回状态码 code = r['code'] AssertUtil().assert_code(code, 200) body = r['body'] # print(body) AssertUtil().assert_in_body(body, '"username": "******"') # 1、初始化数据库对象 conn = init_db('db_1') # 2、查询结果 res_db = conn.fetchone("select id from tb_users where username='******'") print("数据库查询结果:", res_db) # 3、验证 user_id = body['user_id'] assert user_id == res_db['id']
def test_yaml(login): # 初始化url,data url = ConfigYaml().get_conf_url() + login['url'] data = login['data'] # post请求 request = Request() res = request.post(url, json=data)
def test_login(): #3、定义测试数据 conf_y = ConfigYaml() url_path = conf_y.get_conf_url() url = url_path + "/authorizations/" #url = "http://211.103.136.242:8064/authorizations/" data = {"username": "******", "password": "******"} #4、发送POST请求 #r = requests.post(url,json=data) #r = requests_post(url,json=data) request = Request() r = request.post(url, json=data) #5、输出结果 #print(r.json()) print(r) #验证 #返回状态码 code = r["code"] #assert code == 200 AssertUtil().assert_code(code, 200) #返回结果内容 #body = json.dumps(r["body"]) body = r["body"] #assert '"user_id": 1, "username": "******"' in body AssertUtil().assert_in_body(body, '"user_id": 1, "username": "******"') #1、初始化数据库对象 conn = init_db("db_1") #2、查询结果 res_db = conn.fetchone( "select id,username from tb_users where username='******'") print("数据库查询结果", res_db) #3、验证 user_id = body["user_id"] assert user_id == res_db["id"]
def test_login(): # 3. 定义测试数据 url = ConfigYaml().get_conf_url() + "/authorizations/" # url = "http://211.103.136.242:8064/authorizations/" json_data = {"username": "******", "password": "******"} # 4. 发送post请求 # r = requests.post(url, json=json_data) # 调用封装的post方法 # res = requests_post(url, json=json_data) # 调用Requests重构的post方法 request = Request() res = request.post(url, json=json_data) # 5. 输出结果 # print(r.json()) print(res) # 验证 # 返回状态码 code = res["code"] # assert code == 201 AssertUtil().assert_code(code, 200) # 返回结果内容 # body = json.dumps(res["body"]) body = res["body"] # assert '"user_id": 1, "username": "******"' in body AssertUtil().assert_in_body(body, '"user_id": 1, "username": "******"') # 1.初始化数据库对象 conn = init_db("db_1") # 2.查询结果 res_db = conn.fetchone( "select id, username from tb_users where username = '******'") print("数据库查询结果", res_db) # 3. 验证结果 user_id = body["user_id"] assert user_id == res_db["id"]
def test_run(self, case): # 3. 重构函数内容 # data_key = ExcelConfig.DataConfig # run_list第一个用例,根据key获取values url = ConfigYaml().get_conf_url() + case[data_key.url] print(url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] status_code = case[data_key.status_code] db_verify = case[data_key.db_verify] # 1.判断headers是否存在,存在:json转义 不存在:无需作任何操作 if headers: header = json.loads(headers) else: header = headers # 3.判断cookies是否存在,存在:json转义 不存在:无需作任何操作 if cookies: cookie = json.loads(cookies) else: cookie = cookies # 1.验证前置条件是否存在 if pre_exec: # 2.根据前置条件找到前置用例 pre_case = data_init.get_case_pre(pre_exec) print("前置条件信息为:%s" % pre_case) self.run_pre(pre_case) # 2).根据url发送接口请求 request = Request() # params 获取的是字符类型 需要转义成json # 验证params有没有内容 if len(str(params).strip()) is not 0: params = json.loads(params) # method: post / get if str(method).lower() == "get": # 2.增加headers res = request.get(url, json=params, headers=header, cookies=cookie) # 4.增加cookies elif str(method).lower() == "post": res = request.post(url, json=params, headers=header, cookies=cookie) else: log.error("错误请求method:%s" % method) print(res)
def test_yaml(login): """ 执行测试用例 """ uri = login['url'] print(uri) data = login['data'] print(data) request = Request(ConfigYaml().get_config_url()) res = request.post(uri, json=data) print(res)
def test_yaml(login): #初始化url,data url = ConfigYaml().get_conf_url() + login["url"] print("url %s" % url) data = login["data"] print("data %s" % data) #post请求 request = Request() res = request.post(url, json=data) #打印结果 print(res)
def run_api(self, url, method, params=None, header=None, cookie=None): """ 发送请求api """ # 2).接口请求 request = Request() # params 转义json # 验证params有没有内容 if len(str(params).strip()) is not 0: params = json.loads(params) if str(method).lower() == "get": res = request.get(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "post": res = request.post(url, json=params, headers=header, cookies=cookie) else: log.error("错误请求method:%s" % method) return res
def run_api(self, url, method, params=None, header=None, cookie=None): """ 发送请求api :return: """ request = Request() if len(str(params).strip()) is not 0: params = json.loads(params) if str(method).lower() == "get": # 增加Headers和cookies res = request.get(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "post": res = request.post(url, json=params, headers=header, cookies=cookie) else: log.error("错误请求method: %s" % method) return res
def run_api(self, url, method, params=None, header=None, cookie=None): """发送请求api""" # 接口请求 request = Request() # params转义json if len(str(params).strip()) is not 0: params = json.loads(params) # method post、get if str(method).lower() == "get": # 增加了headers res = request.get(url, json=params, headers=header, cooikes=cookie) elif str(method).lower() == "post": res = request.post(url, json=params, headers=header, cooikes=cookie) else: log.error("错误的请求method %s" % method) # print(res) return res
def run_api(self, url, method, params=None, header=None, cookie=None): """ 发送请求api :return: """ # 2).根据url发送接口请求 request = Request() # params 获取的是字符类型 需要转义成json # 验证params有没有内容 if len(str(params).strip()) is not 0: params = json.loads(params) # method: post / get if str(method).lower() == "get": # 2.增加headers res = request.get(url, json=params, headers=header, cookies=cookie) # 4.增加cookies elif str(method).lower() == "post": res = request.post(url, json=params, headers=header, cookies=cookie) else: log.error("错误请求method:%s" % method) return res
def run_api(self, url, params, method='get', header=None, cookie=None): """ 发送请求 """ request = Request(ConfigYaml().get_config_url()) if not len(str(params).strip()): return params params = json.loads(params) if str(method).lower() == "get": res = request.get(url, json=params, headers=header, cookies=cookie) log.debug('get请求') elif str(method).lower() == "post": res = request.post(url, json=params, headers=header, cookies=cookie) log.debug('post请求') else: log.error(f"错误的请求方法:{method}") return res
def run_api(self, url, method, params=None, header=None, cookie=None): """ 发送请求api :return: """ # 接口请求 request = Request() # params 转义json #验证params 有没有内容 if len(str(params).strip()) != 0: params = json.loads(params) #method post/get if str(method).lower() == "get": #增加headers,cookies res = request.get(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "post": res = request.post(url, json=params, headers=header, cookies=cookie) else: log.error("错误请求method: %S" % method) # print(res) return res