mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-10 23:12:41 +08:00
feat: add with_project_meta for runner
This commit is contained in:
@@ -1,36 +1,34 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
from httprunner.api import HttpRunner
|
from httprunner.runner import HttpRunner
|
||||||
from httprunner.schema import ProjectMeta, TestCase
|
from httprunner.schema import ProjectMeta, TestCase
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
runner = HttpRunner()
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/hrun/debug/testcase", tags=["debug"])
|
@router.post("/hrun/debug/testcase", tags=["debug"])
|
||||||
async def debug_single_testcase(project_meta: ProjectMeta, testcase: TestCase):
|
async def debug_single_testcase(project_meta: ProjectMeta, testcase: TestCase):
|
||||||
resp = {"code": 0, "message": "success", "result": {}}
|
resp = {"code": 0, "message": "success", "result": {}}
|
||||||
|
|
||||||
project_meta_json = project_meta.dict(by_alias=True)
|
|
||||||
if project_meta.debugtalk_py:
|
if project_meta.debugtalk_py:
|
||||||
origin_local_keys = list(locals().keys()).copy()
|
origin_local_keys = list(locals().keys()).copy()
|
||||||
exec(project_meta.debugtalk_py, {}, locals())
|
exec(project_meta.debugtalk_py, {}, locals())
|
||||||
new_local_keys = list(locals().keys()).copy()
|
new_local_keys = list(locals().keys()).copy()
|
||||||
new_added_keys = set(new_local_keys) - set(origin_local_keys)
|
new_added_keys = set(new_local_keys) - set(origin_local_keys)
|
||||||
new_added_keys.remove("origin_local_keys")
|
new_added_keys.remove("origin_local_keys")
|
||||||
project_meta_json["functions"] = {}
|
|
||||||
for func_name in new_added_keys:
|
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)
|
config = testcase.config
|
||||||
tests_mapping = {"project_meta": project_meta_json, "testcases": [testcase_json]}
|
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["code"] = 1
|
||||||
resp["message"] = "fail"
|
resp["message"] = "fail"
|
||||||
|
|
||||||
resp["result"] = summary
|
resp["result"] = summary.dict()
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ from httprunner.schema import (
|
|||||||
TestCaseSummary,
|
TestCaseSummary,
|
||||||
TestCaseTime,
|
TestCaseTime,
|
||||||
TestCaseInOut,
|
TestCaseInOut,
|
||||||
|
ProjectMeta,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -37,10 +38,18 @@ class HttpRunner(object):
|
|||||||
self.session_variables: Dict = {}
|
self.session_variables: Dict = {}
|
||||||
self.success: bool = True # indicate testcase execution result
|
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.start_at = 0
|
||||||
self.duration = 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":
|
def with_variables(self, **variables: VariablesMapping) -> "HttpRunner":
|
||||||
self.config.variables.update(variables)
|
self.config.variables.update(variables)
|
||||||
return self
|
return self
|
||||||
@@ -52,7 +61,7 @@ class HttpRunner(object):
|
|||||||
# parse
|
# parse
|
||||||
request_dict = step.request.dict()
|
request_dict = step.request.dict()
|
||||||
parsed_request_dict = parse_data(
|
parsed_request_dict = parse_data(
|
||||||
request_dict, step.variables, self.project_data.functions
|
request_dict, step.variables, self.project_meta.functions
|
||||||
)
|
)
|
||||||
|
|
||||||
# prepare arguments
|
# prepare arguments
|
||||||
@@ -104,7 +113,7 @@ class HttpRunner(object):
|
|||||||
validators = step.validators
|
validators = step.validators
|
||||||
try:
|
try:
|
||||||
resp_obj.validate(
|
resp_obj.validate(
|
||||||
validators, variables_mapping, self.project_data.functions
|
validators, variables_mapping, self.project_meta.functions
|
||||||
)
|
)
|
||||||
self.session.data.success = True
|
self.session.data.success = True
|
||||||
except ValidationFailure:
|
except ValidationFailure:
|
||||||
@@ -126,7 +135,7 @@ class HttpRunner(object):
|
|||||||
step_data = StepData(name=step.name)
|
step_data = StepData(name=step.name)
|
||||||
step_variables = step.variables
|
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)
|
_, testcase_obj = load_testcase_file(ref_testcase_path)
|
||||||
|
|
||||||
case_result = (
|
case_result = (
|
||||||
@@ -169,7 +178,7 @@ class HttpRunner(object):
|
|||||||
step.variables.update(self.session_variables)
|
step.variables.update(self.session_variables)
|
||||||
# parse variables
|
# parse variables
|
||||||
step.variables = parse_variables_mapping(
|
step.variables = parse_variables_mapping(
|
||||||
step.variables, self.project_data.functions
|
step.variables, self.project_meta.functions
|
||||||
)
|
)
|
||||||
# run step
|
# run step
|
||||||
extract_mapping = self.__run_step(step)
|
extract_mapping = self.__run_step(step)
|
||||||
|
|||||||
@@ -73,11 +73,11 @@ class TestCase(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class ProjectMeta(BaseModel):
|
class ProjectMeta(BaseModel):
|
||||||
debugtalk_py: Text = ""
|
debugtalk_py: Text = "" # debugtalk.py file content
|
||||||
functions: FunctionsMapping = {}
|
functions: FunctionsMapping = {}
|
||||||
env: Env = {}
|
env: Env = {}
|
||||||
PWD: Text = os.getcwd()
|
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):
|
class TestsMapping(BaseModel):
|
||||||
|
|||||||
Reference in New Issue
Block a user