From 0647bd4e2542971e1eabbd5fdf3d64583dd29a63 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 15 May 2020 14:53:05 +0800 Subject: [PATCH] feat: add with_project_meta for runner --- httprunner/app/routers/debug.py | 18 ++++++++---------- httprunner/runner.py | 19 ++++++++++++++----- httprunner/schema.py | 4 ++-- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/httprunner/app/routers/debug.py b/httprunner/app/routers/debug.py index 0afe7e22..51dc2276 100644 --- a/httprunner/app/routers/debug.py +++ b/httprunner/app/routers/debug.py @@ -1,36 +1,34 @@ from fastapi import APIRouter -from httprunner.api import HttpRunner +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": {}} - project_meta_json = project_meta.dict(by_alias=True) 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") - project_meta_json["functions"] = {} for func_name in new_added_keys: - project_meta_json["functions"][func_name] = locals()[func_name] + project_meta.functions[func_name] = locals()[func_name] - testcase_json = testcase.dict(by_alias=True) - tests_mapping = {"project_meta": project_meta_json, "testcases": [testcase_json]} + config = testcase.config + teststeps = testcase.teststeps + runner = HttpRunner(config, teststeps).with_project_meta(project_meta).run() + summary = runner.get_summary() - summary = runner.run_tests(tests_mapping) - if not summary["success"]: + if not summary.success: resp["code"] = 1 resp["message"] = "fail" - resp["result"] = summary + resp["result"] = summary.dict() return resp diff --git a/httprunner/runner.py b/httprunner/runner.py index c8b41f8c..235fdb26 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -19,6 +19,7 @@ from httprunner.schema import ( TestCaseSummary, TestCaseTime, TestCaseInOut, + ProjectMeta, ) @@ -37,10 +38,18 @@ class HttpRunner(object): self.session_variables: Dict = {} self.success: bool = True # indicate testcase execution result - self.project_data = load_project_meta(self.config.path) + if self.config.path: + self.project_meta = load_project_meta(self.config.path) + else: + self.project_meta = ProjectMeta() + self.start_at = 0 self.duration = 0 + def with_project_meta(self, project_meta: ProjectMeta) -> "HttpRunner": + self.project_meta = project_meta + return self + def with_variables(self, **variables: VariablesMapping) -> "HttpRunner": self.config.variables.update(variables) return self @@ -52,7 +61,7 @@ class HttpRunner(object): # parse request_dict = step.request.dict() parsed_request_dict = parse_data( - request_dict, step.variables, self.project_data.functions + request_dict, step.variables, self.project_meta.functions ) # prepare arguments @@ -104,7 +113,7 @@ class HttpRunner(object): validators = step.validators try: resp_obj.validate( - validators, variables_mapping, self.project_data.functions + validators, variables_mapping, self.project_meta.functions ) self.session.data.success = True except ValidationFailure: @@ -126,7 +135,7 @@ class HttpRunner(object): step_data = StepData(name=step.name) step_variables = step.variables - ref_testcase_path = os.path.join(self.project_data.PWD, step.testcase) + ref_testcase_path = os.path.join(self.project_meta.PWD, step.testcase) _, testcase_obj = load_testcase_file(ref_testcase_path) case_result = ( @@ -169,7 +178,7 @@ class HttpRunner(object): step.variables.update(self.session_variables) # parse variables step.variables = parse_variables_mapping( - step.variables, self.project_data.functions + step.variables, self.project_meta.functions ) # run step extract_mapping = self.__run_step(step) diff --git a/httprunner/schema.py b/httprunner/schema.py index f9373a5a..7532f77d 100644 --- a/httprunner/schema.py +++ b/httprunner/schema.py @@ -73,11 +73,11 @@ class TestCase(BaseModel): class ProjectMeta(BaseModel): - debugtalk_py: Text = "" + debugtalk_py: Text = "" # debugtalk.py file content functions: FunctionsMapping = {} env: Env = {} PWD: Text = os.getcwd() - test_path: Text = None # run with specified test path + test_path: Text = None # run with specified test path class TestsMapping(BaseModel):