mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-29 03:57:22 +08:00
feat: run referenced testcase in v3
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
config:
|
||||||
|
name: "request methods testcase: reference testcase"
|
||||||
|
variables:
|
||||||
|
foo1: session_bar1
|
||||||
|
base_url: "https://postman-echo.com"
|
||||||
|
verify: False
|
||||||
|
|
||||||
|
teststeps:
|
||||||
|
-
|
||||||
|
name: request with variables
|
||||||
|
variables:
|
||||||
|
foo1: override_bar1
|
||||||
|
testcase: request_methods/request_with_variables.yml
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
from examples.postman_echo import debugtalk
|
||||||
|
from examples.postman_echo.request_methods.validate_with_variables_test \
|
||||||
|
import TestCaseRequestMethodsValidateWithVariables
|
||||||
|
from httprunner.runner import TestCaseRunner
|
||||||
|
from httprunner.schema import TestsConfig, TestStep
|
||||||
|
|
||||||
|
|
||||||
|
class TestCaseRequestMethodsRefTestcase(TestCaseRunner):
|
||||||
|
config = TestsConfig(**{
|
||||||
|
"name": "request methods testcase: reference testcase",
|
||||||
|
"variables": {
|
||||||
|
"foo1": "session_bar1"
|
||||||
|
},
|
||||||
|
"functions": {
|
||||||
|
"get_httprunner_version": debugtalk.get_httprunner_version,
|
||||||
|
"sum_two": debugtalk.sum_two
|
||||||
|
},
|
||||||
|
"base_url": "https://postman-echo.com",
|
||||||
|
"verify": False
|
||||||
|
})
|
||||||
|
|
||||||
|
teststeps = [
|
||||||
|
TestStep(**{
|
||||||
|
"name": "get with params",
|
||||||
|
"variables": {
|
||||||
|
"foo1": "override_bar1"
|
||||||
|
},
|
||||||
|
"testcase": TestCaseRequestMethodsValidateWithVariables
|
||||||
|
})
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
TestCaseRequestMethodsRefTestcase().run()
|
||||||
@@ -96,4 +96,4 @@ class TestCaseRequestMethodsValidateWithVariables(TestCaseRunner):
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
runner = TestCaseRequestMethodsValidateWithVariables().run()
|
runner = TestCaseRequestMethodsValidateWithVariables().run()
|
||||||
print(runner.case_datas)
|
print(runner.step_datas)
|
||||||
|
|||||||
+32
-7
@@ -4,7 +4,7 @@ from loguru import logger
|
|||||||
|
|
||||||
from httprunner import utils
|
from httprunner import utils
|
||||||
from httprunner.client import HttpSession
|
from httprunner.client import HttpSession
|
||||||
from httprunner.exceptions import ValidationFailure
|
from httprunner.exceptions import ValidationFailure, ParamsError
|
||||||
from httprunner.parser import build_url, parse_data, parse_variables_mapping
|
from httprunner.parser import build_url, parse_data, parse_variables_mapping
|
||||||
from httprunner.response import ResponseObject
|
from httprunner.response import ResponseObject
|
||||||
from httprunner.schema import TestsConfig, TestStep, VariablesMapping, TestCase, SessionData
|
from httprunner.schema import TestsConfig, TestStep, VariablesMapping, TestCase, SessionData
|
||||||
@@ -17,6 +17,7 @@ class TestCaseRunner(object):
|
|||||||
session: HttpSession = None
|
session: HttpSession = None
|
||||||
step_datas: List[SessionData] = []
|
step_datas: List[SessionData] = []
|
||||||
validation_results: Dict = {}
|
validation_results: Dict = {}
|
||||||
|
session_variables: Dict = {}
|
||||||
|
|
||||||
def init(self, testcase: TestCase) -> "TestCaseRunner":
|
def init(self, testcase: TestCase) -> "TestCaseRunner":
|
||||||
self.config = testcase.config
|
self.config = testcase.config
|
||||||
@@ -31,9 +32,8 @@ class TestCaseRunner(object):
|
|||||||
self.config.variables.update(variables)
|
self.config.variables.update(variables)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __run_step(self, step: TestStep):
|
def __run_step_request(self, step: TestStep):
|
||||||
logger.info(f"run step: {step.name}")
|
"""run teststep: request"""
|
||||||
|
|
||||||
# parse
|
# parse
|
||||||
request_dict = step.request.dict()
|
request_dict = step.request.dict()
|
||||||
parsed_request_dict = parse_data(request_dict, step.variables, self.config.functions)
|
parsed_request_dict = parse_data(request_dict, step.variables, self.config.functions)
|
||||||
@@ -100,24 +100,49 @@ class TestCaseRunner(object):
|
|||||||
|
|
||||||
return extract_mapping
|
return extract_mapping
|
||||||
|
|
||||||
|
def __run_step_testcase(self, step):
|
||||||
|
"""run teststep: referenced testcase"""
|
||||||
|
step_variables = step.variables
|
||||||
|
testcase: TestCaseRunner = step.testcase
|
||||||
|
res = testcase.with_variables(**step_variables).run()
|
||||||
|
return res.get_export_variables()
|
||||||
|
|
||||||
|
def __run_step(self, step: TestStep):
|
||||||
|
logger.info(f"run step: {step.name}")
|
||||||
|
if step.request:
|
||||||
|
return self.__run_step_request(step)
|
||||||
|
elif step.testcase:
|
||||||
|
return self.__run_step_testcase(step)
|
||||||
|
|
||||||
def test_start(self):
|
def test_start(self):
|
||||||
"""main entrance"""
|
"""main entrance"""
|
||||||
self.step_datas.clear()
|
self.step_datas.clear()
|
||||||
session_variables = {}
|
self.session_variables.clear()
|
||||||
for step in self.teststeps:
|
for step in self.teststeps:
|
||||||
# update with config variables
|
# update with config variables
|
||||||
step.variables.update(self.config.variables)
|
step.variables.update(self.config.variables)
|
||||||
# update with session variables extracted from former step
|
# update with session variables extracted from former step
|
||||||
step.variables.update(session_variables)
|
step.variables.update(self.session_variables)
|
||||||
# parse variables
|
# parse variables
|
||||||
step.variables = parse_variables_mapping(step.variables, self.config.functions)
|
step.variables = parse_variables_mapping(step.variables, self.config.functions)
|
||||||
# run step
|
# run step
|
||||||
extract_mapping = self.__run_step(step)
|
extract_mapping = self.__run_step(step)
|
||||||
# save extracted variables to session variables
|
# save extracted variables to session variables
|
||||||
session_variables.update(extract_mapping)
|
self.session_variables.update(extract_mapping)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""main entrance alias for test_start"""
|
"""main entrance alias for test_start"""
|
||||||
return self.test_start()
|
return self.test_start()
|
||||||
|
|
||||||
|
def get_export_variables(self):
|
||||||
|
export_vars_mapping = {}
|
||||||
|
for var_name in self.config.export:
|
||||||
|
if var_name not in self.session_variables:
|
||||||
|
raise ParamsError(
|
||||||
|
f"failed to export variable {var_name} from session variables {self.session_variables}")
|
||||||
|
|
||||||
|
export_vars_mapping[var_name] = self.session_variables[var_name]
|
||||||
|
|
||||||
|
return export_vars_mapping
|
||||||
|
|||||||
@@ -57,7 +57,8 @@ class Request(BaseModel):
|
|||||||
|
|
||||||
class TestStep(BaseModel):
|
class TestStep(BaseModel):
|
||||||
name: Name
|
name: Name
|
||||||
request: Request
|
request: Request = None
|
||||||
|
testcase: Union[Text, Callable] = None
|
||||||
variables: VariablesMapping = {}
|
variables: VariablesMapping = {}
|
||||||
extract: Dict[Text, Text] = {}
|
extract: Dict[Text, Text] = {}
|
||||||
validators: Validators = Field([], alias="validate")
|
validators: Validators = Field([], alias="validate")
|
||||||
|
|||||||
Reference in New Issue
Block a user