feat: add with_project_meta for runner

This commit is contained in:
debugtalk
2020-05-15 14:53:05 +08:00
parent 5323d9f5c2
commit 0647bd4e25
3 changed files with 24 additions and 17 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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):