mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
fix #139: hooks execution order
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
from httprunner.exception import MyBaseError
|
||||||
|
|
||||||
|
|
||||||
class EventHook(object):
|
class EventHook(object):
|
||||||
"""
|
"""
|
||||||
@@ -14,17 +16,20 @@ class EventHook(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._handlers = set()
|
self._handlers = []
|
||||||
|
|
||||||
def __iadd__(self, handler):
|
def __iadd__(self, handler):
|
||||||
self._handlers.add(handler)
|
self._handlers.append(handler)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __isub__(self, handler):
|
def __isub__(self, handler):
|
||||||
self._handlers.remove(handler)
|
if handler not in self._handlers:
|
||||||
|
raise MyBaseError("handler not found: {}".format(handler))
|
||||||
|
|
||||||
|
index = self._handlers.index(handler)
|
||||||
|
self._handlers.pop(index)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def fire(self, **kwargs):
|
def fire(self, **kwargs):
|
||||||
for handler in self._handlers:
|
for handler in self._handlers:
|
||||||
handler(**kwargs)
|
handler(**kwargs)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
def setup_hook_add_kwargs(method, url, kwargs):
|
||||||
|
kwargs["key"] = "value"
|
||||||
|
|
||||||
|
def setup_hook_remove_kwargs(method, url, kwargs):
|
||||||
|
kwargs.pop("key")
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
- config:
|
||||||
|
name: basic test with httpbin
|
||||||
|
request:
|
||||||
|
base_url: https://httpbin.org/
|
||||||
|
|
||||||
|
- test:
|
||||||
|
name: headers
|
||||||
|
request:
|
||||||
|
url: /headers
|
||||||
|
method: GET
|
||||||
|
setup_hooks:
|
||||||
|
- setup_hook_add_kwargs
|
||||||
|
- setup_hook_remove_kwargs
|
||||||
|
validate:
|
||||||
|
- eq: ["status_code", 200]
|
||||||
|
- eq: [content.headers.Host, "httpbin.org"]
|
||||||
@@ -75,6 +75,13 @@ class TestRunner(ApiServerUnittest):
|
|||||||
# check if teardown function executed
|
# check if teardown function executed
|
||||||
self.assertGreater(end_time - start_time, 2)
|
self.assertGreater(end_time - start_time, 2)
|
||||||
|
|
||||||
|
def test_run_testset_with_setup_hooks(self):
|
||||||
|
testcase_file_path = os.path.join(
|
||||||
|
os.getcwd(), 'tests/httpbin/hooks.yml')
|
||||||
|
runner = HttpRunner().run(testcase_file_path)
|
||||||
|
summary = runner.summary
|
||||||
|
self.assertTrue(summary["success"])
|
||||||
|
|
||||||
def test_run_testset_hardcode(self):
|
def test_run_testset_hardcode(self):
|
||||||
for testcase_file_path in self.testcase_file_path_list:
|
for testcase_file_path in self.testcase_file_path_list:
|
||||||
runner = HttpRunner().run(testcase_file_path)
|
runner = HttpRunner().run(testcase_file_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user