From 41be67b6272d4dc66a7979f390f979b2ef8e3da1 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 11 May 2018 00:44:08 +0800 Subject: [PATCH] fix #173: testset hook in config block --- httprunner/runner.py | 16 ++++++++++-- tests/debugtalk.py | 3 +++ tests/httpbin/hooks.yml | 4 +++ tests/test_runner.py | 55 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/httprunner/runner.py b/httprunner/runner.py index 962ff66c..6ca7160e 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -16,6 +16,18 @@ class Runner(object): config_dict = config_dict or {} self.init_config(config_dict, "testset") + # testset setup hooks + testset_setup_hooks = config_dict.pop("setup_hooks", []) + if testset_setup_hooks: + self.do_hook_actions(testset_setup_hooks) + + # testset teardown hooks + self.testset_teardown_hooks = config_dict.pop("teardown_hooks", []) + + def __del__(self): + if self.testset_teardown_hooks: + self.do_hook_actions(self.testset_teardown_hooks) + def init_config(self, config_dict, level): """ create/update context variables binds @param (dict) config_dict @@ -130,7 +142,7 @@ class Runner(object): # prepare parsed_request = self.init_config(testcase_dict, level="testcase") - self.context.bind_variables({"request": parsed_request}) + self.context.bind_variables({"request": parsed_request}, level="testcase") try: url = parsed_request.pop('url') @@ -158,7 +170,7 @@ class Runner(object): # teardown hooks teardown_hooks = testcase_dict.get("teardown_hooks", []) if teardown_hooks: - self.context.bind_variables({"response": resp}) + self.context.bind_variables({"response": resp}, level="testcase") self.do_hook_actions(teardown_hooks) # extract diff --git a/tests/debugtalk.py b/tests/debugtalk.py index 619b8eab..5ba51598 100644 --- a/tests/debugtalk.py +++ b/tests/debugtalk.py @@ -81,3 +81,6 @@ def teardown_hook_sleep_N_secs(response, n_secs): time.sleep(0.1) else: time.sleep(n_secs) + +def hook_print(msg): + print(msg) diff --git a/tests/httpbin/hooks.yml b/tests/httpbin/hooks.yml index c30df0b7..bdfca129 100644 --- a/tests/httpbin/hooks.yml +++ b/tests/httpbin/hooks.yml @@ -2,6 +2,10 @@ name: basic test with httpbin request: base_url: http://127.0.0.1:3458/ + setup_hooks: + - ${hook_print(setup)} + teardown_hooks: + - ${hook_print(teardown)} - test: name: headers diff --git a/tests/test_runner.py b/tests/test_runner.py index aa518575..35be9469 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -69,7 +69,60 @@ class TestRunner(ApiServerUnittest): with self.assertRaises(exception.ValidationError): self.test_runner.run_test(test) - def test_run_testset_with_setup_hooks(self): + def test_run_testset_with_hooks(self): + testcase_file_path = os.path.join( + os.getcwd(), 'tests/httpbin/hooks.yml') + + start_time = time.time() + + config_dict = { + "path": os.path.join(os.getcwd(), __file__), + "name": "basic test with httpbin", + "request": { + "base_url": "http://127.0.0.1:3458/" + }, + "setup_hooks": [ + "${sleep_N_secs(0.5)}" + "${hook_print(setup)}" + ], + "teardown_hooks": [ + "${sleep_N_secs(1)}", + "${hook_print(teardown)}" + ] + } + test = { + "name": "get token", + "request": { + "url": "http://127.0.0.1:5000/api/get-token", + "method": "POST", + "headers": { + "content-type": "application/json", + "user_agent": "iOS/10.3", + "device_sn": "HZfFBh6tU59EdXJ", + "os_platform": "ios", + "app_version": "2.8.6" + }, + "json": { + "sign": "f1219719911caae89ccc301679857ebfda115ca2" + } + }, + "validate": [ + {"check": "status_code", "expect": 200} + ] + } + test_runner = runner.Runner(config_dict) + end_time = time.time() + # check if testset setup hook executed + self.assertGreater(end_time - start_time, 0.5) + + start_time = time.time() + test_runner.run_test(test) + test_runner.run_test(test) + end_time = time.time() + # testset teardown hook has not been executed now + self.assertLess(end_time - start_time, 1) + + def test_run_httprunner_with_hooks(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/httpbin/hooks.yml')