diff --git a/examples/httpbin/hooks_test.py b/examples/httpbin/hooks_test.py index 7fa92cf1..0e86ee6b 100644 --- a/examples/httpbin/hooks_test.py +++ b/examples/httpbin/hooks_test.py @@ -1,5 +1,6 @@ # NOTICE: Generated By HttpRunner. DO NOT EDIT! # FROM: examples/httpbin/hooks.yml + from httprunner import HttpRunner, TConfig, TStep @@ -11,6 +12,7 @@ class TestCaseHooks(HttpRunner): "setup_hooks": ["${hook_print(setup)}"], "teardown_hooks": ["${hook_print(teardown)}"], "path": "examples/httpbin/hooks_test.py", + "variables": {}, } ) diff --git a/httprunner/compat.py b/httprunner/compat.py index ae3dd76b..7f1fa2c3 100644 --- a/httprunner/compat.py +++ b/httprunner/compat.py @@ -104,7 +104,16 @@ def sort_request_by_custom_order(request: Dict) -> Dict: def sort_step_by_custom_order(step: Dict) -> Dict: - custom_order = ["name", "variables", "request", "testcase", "extract", "validate"] + custom_order = [ + "name", + "variables", + "request", + "testcase", + "setup_hooks", + "teardown_hooks", + "extract", + "validate", + ] return sort_dict_by_custom_order(step, custom_order) @@ -116,6 +125,12 @@ def ensure_step_attachment(step: Dict) -> Dict: if "variables" in step: test_dict["variables"] = step["variables"] + if "setup_hooks" in step: + test_dict["setup_hooks"] = step["setup_hooks"] + + if "teardown_hooks" in step: + test_dict["teardown_hooks"] = step["teardown_hooks"] + if "extract" in step: test_dict["extract"] = convert_extractors(step["extract"]) diff --git a/httprunner/schema.py b/httprunner/schema.py index 90211c42..227e4f20 100644 --- a/httprunner/schema.py +++ b/httprunner/schema.py @@ -37,7 +37,7 @@ class TConfig(BaseModel): name: Name verify: Verify = False base_url: BaseUrl = "" - # Text: prepare variables in debugtalk.py, ${get_variable()} + # Text: prepare variables in debugtalk.py, ${gen_variables()} variables: Union[VariablesMapping, Text] = {} setup_hooks: Hook = [] teardown_hooks: Hook = [] @@ -66,6 +66,8 @@ class TStep(BaseModel): request: Request = None testcase: Union[Text, Callable] = "" variables: VariablesMapping = {} + setup_hooks: Hook = [] + teardown_hooks: Hook = [] extract: Dict[Text, Text] = {} validators: Validators = Field([], alias="validate")