def put_request(url, data, header=None, cookies=None): """ Put请求 :param url: :param data: :param header: :return: """ if not (url.startswith('http://') or url.startswith('https://')): url = '%s%s' % ('http://', url) log_tool.debug(url) try: if data is None: response = requests.put(url=url, headers=header, cookies=cookies) else: response = requests.put(url=url, params=data, headers=header, cookies=cookies) except requests.RequestException as e: log_tool.error('%s%s' % ('RequestException url: ', url)) log_tool.error(e) return () except Exception as e: print('%s%s' % ('Exception url: ', url)) print(e) return () time_consuming = response.elapsed.microseconds / 1000 log_tool.info('----请求用时: %s 秒数' % time_consuming) return response
def invoke(cmd): try: output, errors = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() o = output.decode("utf-8") return o except Exception as e: log_tool.error('执行命令失败,请检查环境配置') log_tool.error(e) raise
def assert_not_null(actual): """ 验证实际结果不为null :param actual: :return: """ try: assert actual != '' return True except: log_tool.error("预期不为空,实际结果为空") raise
def assert_equal(body, expected_msg): """ 验证response body中是否等于预期字符串 :param body: :param expected_msg: :return: """ try: assert body == expected_msg return True except: log_tool.error( "Response body != expected_msg, expected_msg is %s, body is %s" % (expected_msg, body)) raise
def assert_in(body, expected_msg): """ 验证response body中是否包含预期字符串 :param body: :param expected_msg: :return: """ try: text = json.dumps(body, ensure_ascii=False) # print(text) assert expected_msg in text return True except: log_tool.error("不包含期望值, 期望值 是 %s" % expected_msg) raise
def assert_time(time, expected_time): """ 验证response body响应时间小于预期最大响应时间,单位:毫秒 :param body: :param expected_time: :return: """ try: assert time < expected_time return True except: log_tool.error( "Response time > expected_time, expected_time is %s, time is %s" % (expected_time, time)) raise
def d(): # 用户登录 json = {"pwd": "VzuT98XI", "userName": "******"} r = requests.request("POST", BASE_URL + "/login", json=json) token = r.json()["data"]["token"] log_tool.info("-------------------用户登录(请求)-------------------" + STR_REQUEST.format(r.request.method, r.request.url, r.request.headers, r.request.body)) log_tool.info("-------------------用户登录(响应)-------------------" + STR_RESPONSE.format(r.status_code, r.headers, r.text)) try: assert "2000" in r.text except: log_tool.error(r.text) print(r.text) return {"pwd": "VzuT98XI", "name": "pafef09020", "token": token}
def test_lock_t(d): data = {"userName": d["name"]} token = {"token": d["token"]} r = requests.request("POST", BASE_URL + "/user/lock", data=data, headers=token) log_tool.info("-------------------冻结用户test_lock_t(请求)-------------------" + STR_REQUEST.format(r.request.method, r.request.url, r.request.headers, r.request.body)) log_tool.info("-------------------冻结用户test_lock_t(响应)-------------------" + STR_RESPONSE.format(r.status_code, r.headers, r.text)) print(r.text) try: assert "2000" in r.text except: log_tool.error(r.text)
def get_request(url, params=None, headers=None, cookies=None): """ Get请求 :param url: :param data: :param header: :return: """ if not (url.startswith('http://') or url.startswith('https://')): url = '%s%s' % ('http://', url) print(url) try: response = requests.get(url=url, params=params, headers=headers, cookies=cookies) except requests.RequestException as e: log_tool.error('%s%s' % ('Exception url: ', url)) log_tool.error(e) return () except Exception as e: log_tool.error('%s%s' % ('Exception url: ', url)) return () time_consuming = response.elapsed.microseconds / 1000 log_tool.info('----请求用时: %s 秒数' % time_consuming) return response
def test__product_addProd(d): brand = random_tool.random_numbers() colors = random_tool.random_color() json = { "brand": "huawei"+brand, "colors": colors, "price": 0, "productCode": "string", "productName": "string", "sizes": ["string"], "type": "string" } token = {"token":d["token"]} r = requests.request("POST",BASE_URL + "/product/addProd",json = json,headers = token) print(r.request.body) log_tool.info("-------------------增加商品test__product_addProd(请求)-------------------" + STR_REQUEST.format(r.request.method, r.request.url, r.request.headers, r.request.body)) log_tool.info("-------------------增加商品test__product_addProd(响应)-------------------" + STR_RESPONSE.format(r.status_code, r.headers, r.text)) print(r.text) try: assert "2000" in r.text except: log_tool.error(r.text)
def test_changepwd_t(d): json = { "newPwd": d["pwd"], "oldPwd": d["pwd"], "reNewPwd": d["pwd"], "userName": d["name"] } token = {"token": d["token"]} r = requests.request("POST", BASE_URL + "/user/changepwd", json=json, headers=token) log_tool.info( "-------------------修改密码test_changepwd_t(请求)-------------------" + STR_REQUEST.format(r.request.method, r.request.url, r.request.headers, r.request.body)) log_tool.info( "-------------------修改密码test_changepwd_t(响应)-------------------" + STR_RESPONSE.format(r.status_code, r.headers, r.text)) print(r.text) try: assert "2000" in r.text except: log_tool.error(r.text)
def post_request(url, data=None, files=None, params=None, headers=None, json=None, cookies=None): """ Post请求 :param url: :param data: :param header: :return: """ if not (url.startswith('http://') or url.startswith('https://')): url = '%s%s' % ('http://', url) print(url) try: response = requests.post(url, data=data, files=files, params=params, headers=headers, json=json, cookies=cookies) except requests.RequestException as e: log_tool.error('%s%s' % ('RequestException url: ', url)) log_tool.error(e) return () except Exception as e: log_tool.error('%s%s' % ('Exception url: ', url)) log_tool.error(e) return () # time_consuming为响应时间,单位为毫秒 time_consuming = response.elapsed.microseconds / 1000 log_tool.info('----请求用时: %s 秒数' % time_consuming) return response