class TestHttpRunner(unittest.TestCase): def setUp(self): loader.project_meta = None self.runner = HttpRunner() def test_run_testcase_by_path_request_only(self): self.runner.run_path( "examples/postman_echo/request_methods/request_with_functions.yml" ) result = self.runner.get_summary() self.assertTrue(result.success) self.assertEqual(result.name, "request methods testcase with functions") self.assertEqual(result.step_datas[0].name, "get with params") self.assertEqual(len(result.step_datas), 3) def test_run_testcase_by_path_ref_testcase(self): self.runner.run_path( "examples/postman_echo/request_methods/request_with_testcase_reference.yml" ) result = self.runner.get_summary() self.assertTrue(result.success) self.assertEqual(result.name, "request methods testcase: reference testcase") self.assertEqual(result.step_datas[0].name, "request with functions") self.assertEqual(len(result.step_datas), 2) def test_run_testcase_with_abnormal_path(self): exit_code = main_run(["tests/data/a-b.c/2 3.yml"]) self.assertEqual(exit_code, 0) self.assertTrue(os.path.exists("tests/data/a_b_c/__init__.py")) self.assertTrue(os.path.exists("tests/data/a_b_c/debugtalk.py")) self.assertTrue(os.path.exists("tests/data/a_b_c/T1_test.py")) self.assertTrue(os.path.exists("tests/data/a_b_c/T2_3_test.py"))
class TestHttpRunner(unittest.TestCase): def setUp(self): self.runner = HttpRunner() def test_run_testcase_by_path_request_only(self): self.runner.run_path( "examples/postman_echo/request_methods/request_with_functions.yml") result = self.runner.get_summary() self.assertTrue(result.success) self.assertEqual(result.name, "request methods testcase with functions") self.assertEqual(result.step_datas[0].name, "get with params") self.assertEqual(len(result.step_datas), 3) def test_run_testcase_by_path_ref_testcase(self): self.runner.run_path( "examples/postman_echo/request_methods/request_with_testcase_reference.yml" ) result = self.runner.get_summary() self.assertTrue(result.success) self.assertEqual(result.name, "request methods testcase: reference testcase") self.assertEqual(result.step_datas[0].name, "request with functions") self.assertEqual(len(result.step_datas), 1)
from fastapi import APIRouter from httprunner.runner import HttpRunner from httprunner.schema import ProjectMeta, TestCase router = APIRouter() runner = HttpRunner() @router.post("/hrun/debug/testcase", tags=["debug"]) async def debug_single_testcase(project_meta: ProjectMeta, testcase: TestCase): resp = {"code": 0, "message": "success", "result": {}} if project_meta.debugtalk_py: origin_local_keys = list(locals().keys()).copy() exec(project_meta.debugtalk_py, {}, locals()) new_local_keys = list(locals().keys()).copy() new_added_keys = set(new_local_keys) - set(origin_local_keys) new_added_keys.remove("origin_local_keys") for func_name in new_added_keys: project_meta.functions[func_name] = locals()[func_name] runner.with_project_meta(project_meta).run_testcase(testcase) summary = runner.get_summary() if not summary.success: resp["code"] = 1 resp["message"] = "fail" resp["result"] = summary.dict() return resp
def setUp(self): loader.project_meta = None self.runner = HttpRunner()
def setUp(self): self.runner = HttpRunner()
def test_start(self): s = requests.Session() HttpRunner(self.config, self.teststeps, session=s).with_variables(foo1="bar123", foo2="bar22").run()
def run_step_request(runner: HttpRunner, step: TStep) -> StepResult: """run teststep: request""" step_result = StepResult( name=step.name, success=False, ) start_time = time.time() step.variables = runner.merge_step_variables(step.variables) # parse functions = runner.parser.functions_mapping prepare_upload_step(step, functions) request_dict = step.request.dict() request_dict.pop("upload", None) parsed_request_dict = runner.parser.parse_data(request_dict, step.variables) parsed_request_dict["headers"].setdefault( "HRUN-Request-ID", f"HRUN-{runner.case_id}-{str(int(time.time() * 1000))[-6:]}", ) step.variables["request"] = parsed_request_dict # setup hooks if step.setup_hooks: call_hooks(runner, step.setup_hooks, step.variables, "setup request") # prepare arguments config = runner.get_config() method = parsed_request_dict.pop("method") url_path = parsed_request_dict.pop("url") url = build_url(config.base_url, url_path) parsed_request_dict["verify"] = config.verify parsed_request_dict["json"] = parsed_request_dict.pop("req_json", {}) # request resp = runner.session.request(method, url, **parsed_request_dict) resp_obj = ResponseObject(resp, runner.parser) step.variables["response"] = resp_obj # teardown hooks if step.teardown_hooks: call_hooks(runner, step.teardown_hooks, step.variables, "teardown request") def log_req_resp_details(): err_msg = "\n{} DETAILED REQUEST & RESPONSE {}\n".format( "*" * 32, "*" * 32) # log request err_msg += "====== request details ======\n" err_msg += f"url: {url}\n" err_msg += f"method: {method}\n" headers = parsed_request_dict.pop("headers", {}) err_msg += f"headers: {headers}\n" for k, v in parsed_request_dict.items(): v = utils.omit_long_data(v) err_msg += f"{k}: {repr(v)}\n" err_msg += "\n" # log response err_msg += "====== response details ======\n" err_msg += f"status_code: {resp.status_code}\n" err_msg += f"headers: {resp.headers}\n" err_msg += f"body: {repr(resp.text)}\n" logger.error(err_msg) # extract extractors = step.extract extract_mapping = resp_obj.extract(extractors, step.variables) step_result.export_vars = extract_mapping variables_mapping = step.variables variables_mapping.update(extract_mapping) # validate validators = step.validators try: resp_obj.validate(validators, variables_mapping) step_result.success = True except ValidationFailure: log_req_resp_details() raise finally: session_data = runner.session.data session_data.success = step_result.success session_data.validators = resp_obj.validation_results # save step data step_result.data = session_data step_result.elapsed = time.time() - start_time return step_result