diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 2fb4f91d..2870880e 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,11 @@ # Release History +## 3.1.2 (2020-06-23) + +**Fixed** + +- fix: missing setup/teardown hooks for referenced testcase + ## 3.1.1 (2020-06-23) **Added** @@ -8,7 +14,7 @@ **Fixed** -- fix #942: type_match None +- fix: ValueError when type_match None - fix: override referenced testcase export in teststep - fix: avoid duplicate import - fix: override locust weight diff --git a/examples/postman_echo/request_methods/request_with_functions_test.py b/examples/postman_echo/request_methods/request_with_functions_test.py index 99d3731f..996eed64 100644 --- a/examples/postman_echo/request_methods/request_with_functions_test.py +++ b/examples/postman_echo/request_methods/request_with_functions_test.py @@ -1,4 +1,4 @@ -# NOTE: Generated By HttpRunner v3.1.0 +# NOTE: Generated By HttpRunner v3.1.2 # FROM: request_methods/request_with_functions.yml from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase diff --git a/examples/postman_echo/request_methods/request_with_testcase_reference.yml b/examples/postman_echo/request_methods/request_with_testcase_reference.yml index 7cd6630e..abb51f3e 100644 --- a/examples/postman_echo/request_methods/request_with_testcase_reference.yml +++ b/examples/postman_echo/request_methods/request_with_testcase_reference.yml @@ -13,7 +13,11 @@ teststeps: variables: foo1: testcase_ref_bar1 expect_foo1: testcase_ref_bar1 + setup_hooks: + - ${sleep(0.1)} testcase: request_methods/request_with_functions.yml + teardown_hooks: + - ${sleep(0.2)} export: - foo3 - diff --git a/examples/postman_echo/request_methods/request_with_testcase_reference_test.py b/examples/postman_echo/request_methods/request_with_testcase_reference_test.py index 7154992b..2d57795e 100644 --- a/examples/postman_echo/request_methods/request_with_testcase_reference_test.py +++ b/examples/postman_echo/request_methods/request_with_testcase_reference_test.py @@ -1,4 +1,4 @@ -# NOTE: Generated By HttpRunner v3.1.0 +# NOTE: Generated By HttpRunner v3.1.2 # FROM: request_methods/request_with_testcase_reference.yml import sys @@ -33,7 +33,9 @@ class TestCaseRequestWithTestcaseReference(HttpRunner): .with_variables( **{"foo1": "testcase_ref_bar1", "expect_foo1": "testcase_ref_bar1"} ) + .setup_hook("${sleep(0.1)}") .call(RequestWithFunctions) + .teardown_hook("${sleep(0.2)}") .export(*["foo3"]) ), Step( diff --git a/httprunner/__init__.py b/httprunner/__init__.py index 55eeae4b..6118a14a 100644 --- a/httprunner/__init__.py +++ b/httprunner/__init__.py @@ -1,4 +1,4 @@ -__version__ = "3.1.1" +__version__ = "3.1.2" __description__ = "One-stop solution for HTTP(S) testing." # import firstly for monkey patch if needed diff --git a/httprunner/runner.py b/httprunner/runner.py index ab9b4efa..f916a2d7 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -89,7 +89,7 @@ class HttpRunner(object): return self def __call_hooks( - self, hooks: Hooks, step_variables: VariablesMapping, hook_type: Text, + self, hooks: Hooks, step_variables: VariablesMapping, hook_msg: Text, ) -> NoReturn: """ call hook actions. @@ -106,10 +106,10 @@ class HttpRunner(object): request: parsed request dict response: ResponseObject for current response - hook_type: setup/teardown + hook_msg: setup/teardown request/testcase """ - logger.debug(f"call {hook_type} hook actions.") + logger.info(f"call hook actions: {hook_msg}") if not isinstance(hooks, List): logger.error(f"Invalid hooks format: {hooks}") @@ -153,7 +153,7 @@ class HttpRunner(object): # setup hooks if step.setup_hooks: - self.__call_hooks(step.setup_hooks, step.variables, "setup") + self.__call_hooks(step.setup_hooks, step.variables, "setup request") # prepare arguments method = parsed_request_dict.pop("method") @@ -169,7 +169,7 @@ class HttpRunner(object): # teardown hooks if step.teardown_hooks: - self.__call_hooks(step.teardown_hooks, step.variables, "teardown") + self.__call_hooks(step.teardown_hooks, step.variables, "teardown request") def log_req_resp_details(): err_msg = "\n{} DETAILED REQUEST & RESPONSE {}\n".format("*" * 32, "*" * 32) @@ -236,6 +236,10 @@ class HttpRunner(object): step_variables = step.variables step_export = step.export + # setup hooks + if step.setup_hooks: + self.__call_hooks(step.setup_hooks, step_variables, "setup testcase") + if hasattr(step.testcase, "config") and hasattr(step.testcase, "teststeps"): testcase_cls = step.testcase case_result = ( @@ -269,6 +273,10 @@ class HttpRunner(object): f"Invalid teststep referenced testcase: {step.dict()}" ) + # teardown hooks + if step.teardown_hooks: + self.__call_hooks(step.teardown_hooks, step.variables, "teardown testcase") + step_data.data = case_result.get_step_datas() # list of step data step_data.export_vars = case_result.get_export_variables() step_data.success = case_result.success diff --git a/httprunner/testcase.py b/httprunner/testcase.py index f6bb0e84..f8044d95 100644 --- a/httprunner/testcase.py +++ b/httprunner/testcase.py @@ -350,6 +350,14 @@ class StepRefCase(object): def __init__(self, step_context: TStep): self.__step_context = step_context + def teardown_hook(self, hook: Text, assign_var_name: Text = None) -> "StepRefCase": + if assign_var_name: + self.__step_context.teardown_hooks.append({assign_var_name: hook}) + else: + self.__step_context.teardown_hooks.append(hook) + + return self + def export(self, *var_name: Text) -> "StepRefCase": self.__step_context.export.extend(var_name) return self @@ -366,6 +374,14 @@ class RunTestCase(object): self.__step_context.variables.update(variables) return self + def setup_hook(self, hook: Text, assign_var_name: Text = None) -> "RunTestCase": + if assign_var_name: + self.__step_context.setup_hooks.append({assign_var_name: hook}) + else: + self.__step_context.setup_hooks.append(hook) + + return self + def call(self, testcase: Callable) -> StepRefCase: self.__step_context.testcase = testcase return StepRefCase(self.__step_context) diff --git a/pyproject.toml b/pyproject.toml index 5a1e5314..e300377b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "httprunner" -version = "3.1.1" +version = "3.1.2" description = "One-stop solution for HTTP(S) testing." license = "Apache-2.0" readme = "README.md"