def request_method(cls, url, method, param=None, **kwargs): """ 方法作用:封装不同类型的请求方法 url: 接口请求地址url method: 请求的方法 param: 请求参数 kwargs: 其他需要的kwargs ret: 请求返回数据 """ get_logger().info("Request URL:%s", url) get_logger().info("Request METHOD:%s", method) ret = None if method == "POST" or method == "post": ret = requests.post(url, param, **kwargs) elif method == "GET" or method == "get": ret = requests.get(url, param, **kwargs) elif method == "DELETE" or method == "delete": ret = requests.delete(url, **kwargs) elif method == "PUT" or method == "put": ret = requests.put(url, param, **kwargs) if ret.status_code == 500: get_logger().info("Server Internal Error 500") return "Server Internal Error 500" elif ret.status_code == 404: get_logger().info("Cannot find the page 404") return "Cannot find the page 404" else: get_logger().info( "Response data:%s", json.dumps(json.loads(ret.text), sort_keys=True, indent=2)) return json.loads(ret.text)
def delete_all_action(request): if request.method == "POST": test_case_pks = request.POST.getlist('test_case_ids') get_logger().info("删除的用例编号为:%s", test_case_pks) for test_case_pk in test_case_pks: if test_case_pk != '': TestCaseInfo.objects.filter(id=test_case_pk).delete() return redirect('test_case_list')
def execute_action(request): if request.method == "POST": test_case_pk = request.POST["test_case_id"] #test_case = TestCaseInfo.objects.get(id=test_case_pk) #os.environ.setdefault('test_case_id', test_case_pk) os.environ['test_case_id'] = test_case_pk get_logger().info("pytest cmd:%s", OperateTestRunner.get_pytest_cmd()) pytest.main(OperateTestRunner.get_pytest_cmd()) return render(request, 'tpTest/add_test_case.html')
def assert_in_list(expect_value, actual_value): """ IL: 判断预期值在实际值中 """ if expect_value in actual_value: get_logger().info( "=============expect_value in actual_value==============") return True else: get_logger().info( "=============expect_value not in actual_value==============") return False
def test_api_main(url, method, params): get_logger().info( "================================================================================" ) get_logger().info("url为:%s", url) get_logger().info("method为:%s", method) get_logger().info("params为:%s", params) ret = None if method == "post": ret = requests.post(url, json=json.loads(params)) get_logger().info("ret为:%s", json.loads(ret.text)) assert ret
def assert_not_in_list(expect_value, actual_value): """ LEN: 判断预期值不在实际值中 """ if isinstance(actual_value, list): if expect_value in actual_value: get_logger().info( "=============expect_value in actual_value==============") return False else: get_logger().info( "=============expect_value not in actual_value==============") return True
def get_next_step_value(cls, api_response, set_up): """ for api multi-business get next step test data """ next_step_dict = {} temp_dict = json.loads(set_up)["next_step"] for key in temp_dict.keys(): k_v = temp_dict[key] v = OperateDict.get_value_in_dict(key, api_response, [])[0] if isinstance(k_v, list): k_v_v = k_v[0] next_step_dict[k_v_v] = [v] else: next_step_dict[k_v] = v get_logger().info("next_step_value:(%s)", next_step_dict) return json.dumps(next_step_dict)
def get_api_response_data(cls, url, method, params=None, cookies=None, headers=None, files=None): """ url: 接口请求地址url method: 请求的方法 param: 请求参数 kwargs: 其他需要的kwargs ret: 请求返回数据 """ get_logger().info("Request PARAM:%s", params) if cookies: get_logger().info("Request COOKIES:%s", cookies) if headers: get_logger().info("Request HEADERS:%s", headers) ret = None if method == "POST" or method == "post": # 上传文件操作,提交数据参数方式不同处理;content={请求参数格式} if files: get_logger().info("Request FILES:%s", files) datas = {} datas['content'] = params ret = cls.request_method(url, method, datas, cookies=json.loads(cookies), headers=headers, files=files) else: if cookies: ret = cls.request_method(url, method, json=json.loads(params), cookies=json.loads(cookies), headers=headers) else: ret = cls.request_method(url, method, json=json.loads(params), headers=headers) return ret
def checker_api(cls, api_response, checkers): """ For api assert """ flag = True checkers_list = list(json.loads(checkers)["checkers"]) for checker in checkers_list: expect_key = checker["k"] expect_value = checker["v"] compare_type = checker["ct"] actual_value = OperateDict.get_value_in_dict( expect_key, api_response, []) get_logger().info("actual_value:(%s:%s:%s)", expect_key, actual_value, type(actual_value[0])) get_logger().info("expect_value:(%s:%s:%s)", expect_key, expect_value, type(expect_value)) flag = assert_api(expect_value, actual_value, compare_type) if flag == False: get_logger().info("Fail:<%s>", checker) break return flag
def get_test_data(): data_list = [] tc_id = change_list_to_str() test_case = None get_logger().info("需要执行的用例为:%s %s", tc_id, type(tc_id)) if isinstance(tc_id, str): test_case = TestCaseInfo.objects.get(id=tc_id) data_tuple = (test_case.url, test_case.method, test_case.params) data_list.append(data_tuple) elif isinstance(tc_id, list): for i in tc_id: data_tuple = () if i != '': test_case = TestCaseInfo.objects.get(id=i) data_tuple = (test_case.url, test_case.method, test_case.params) data_list.append(data_tuple) get_logger().info("测试数据为:%s", data_list) else: get_logger().info("获取数据conftest.py出错") return data_list
def delete_action(request): if request.method == "POST": get_logger().info("request.POST:%s", request.POST) test_case_pk = request.POST["test_case_id"] TestCaseInfo.objects.filter(id=test_case_pk).delete() return redirect('test_case_list')