mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-06 15:01:22 +08:00
fix #173: testset hook in config block
This commit is contained in:
@@ -16,6 +16,18 @@ class Runner(object):
|
|||||||
config_dict = config_dict or {}
|
config_dict = config_dict or {}
|
||||||
self.init_config(config_dict, "testset")
|
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):
|
def init_config(self, config_dict, level):
|
||||||
""" create/update context variables binds
|
""" create/update context variables binds
|
||||||
@param (dict) config_dict
|
@param (dict) config_dict
|
||||||
@@ -130,7 +142,7 @@ class Runner(object):
|
|||||||
|
|
||||||
# prepare
|
# prepare
|
||||||
parsed_request = self.init_config(testcase_dict, level="testcase")
|
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:
|
try:
|
||||||
url = parsed_request.pop('url')
|
url = parsed_request.pop('url')
|
||||||
@@ -158,7 +170,7 @@ class Runner(object):
|
|||||||
# teardown hooks
|
# teardown hooks
|
||||||
teardown_hooks = testcase_dict.get("teardown_hooks", [])
|
teardown_hooks = testcase_dict.get("teardown_hooks", [])
|
||||||
if 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)
|
self.do_hook_actions(teardown_hooks)
|
||||||
|
|
||||||
# extract
|
# extract
|
||||||
|
|||||||
@@ -81,3 +81,6 @@ def teardown_hook_sleep_N_secs(response, n_secs):
|
|||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
else:
|
else:
|
||||||
time.sleep(n_secs)
|
time.sleep(n_secs)
|
||||||
|
|
||||||
|
def hook_print(msg):
|
||||||
|
print(msg)
|
||||||
|
|||||||
@@ -2,6 +2,10 @@
|
|||||||
name: basic test with httpbin
|
name: basic test with httpbin
|
||||||
request:
|
request:
|
||||||
base_url: http://127.0.0.1:3458/
|
base_url: http://127.0.0.1:3458/
|
||||||
|
setup_hooks:
|
||||||
|
- ${hook_print(setup)}
|
||||||
|
teardown_hooks:
|
||||||
|
- ${hook_print(teardown)}
|
||||||
|
|
||||||
- test:
|
- test:
|
||||||
name: headers
|
name: headers
|
||||||
|
|||||||
@@ -69,7 +69,60 @@ class TestRunner(ApiServerUnittest):
|
|||||||
with self.assertRaises(exception.ValidationError):
|
with self.assertRaises(exception.ValidationError):
|
||||||
self.test_runner.run_test(test)
|
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(
|
testcase_file_path = os.path.join(
|
||||||
os.getcwd(), 'tests/httpbin/hooks.yml')
|
os.getcwd(), 'tests/httpbin/hooks.yml')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user