mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-14 20:08:23 +08:00
refactor hook mechanism:
1, remove EventHook; 2, setup_hooks: could reference request dict; 3, teardown_hooks: could reference Response object.
This commit is contained in:
@@ -68,8 +68,16 @@ def gen_random_string(str_len):
|
||||
random_string = ''.join(random_char_list)
|
||||
return random_string
|
||||
|
||||
def setup_hook_add_kwargs(method, url, kwargs):
|
||||
kwargs["key"] = "value"
|
||||
def setup_hook_add_kwargs(request):
|
||||
request["key"] = "value"
|
||||
|
||||
def setup_hook_remove_kwargs(method, url, kwargs):
|
||||
kwargs.pop("key")
|
||||
def setup_hook_remove_kwargs(request):
|
||||
request.pop("key")
|
||||
|
||||
def teardown_hook_sleep_N_secs(response, n_secs):
|
||||
""" sleep n seconds after request
|
||||
"""
|
||||
if response.status_code == 200:
|
||||
time.sleep(0.1)
|
||||
else:
|
||||
time.sleep(n_secs)
|
||||
|
||||
@@ -9,8 +9,10 @@
|
||||
url: /headers
|
||||
method: GET
|
||||
setup_hooks:
|
||||
- setup_hook_add_kwargs
|
||||
- setup_hook_remove_kwargs
|
||||
- ${setup_hook_add_kwargs($request)}
|
||||
- ${setup_hook_remove_kwargs($request)}
|
||||
teardown_hooks:
|
||||
- ${teardown_hook_sleep_N_secs($response, 1)}
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
- eq: [content.headers.Host, "127.0.0.1:3458"]
|
||||
|
||||
@@ -40,7 +40,9 @@ class TestHttpClient(ApiServerUnittest):
|
||||
self.assertEqual(True, resp.json()['success'])
|
||||
|
||||
def test_prepare_kwargs_content_type_application_json_without_charset(self):
|
||||
kwargs = {
|
||||
request = {
|
||||
"url": "/path",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json"
|
||||
},
|
||||
@@ -49,13 +51,15 @@ class TestHttpClient(ApiServerUnittest):
|
||||
"b": 2
|
||||
}
|
||||
}
|
||||
setup_hook_prepare_kwargs("POST", "/path", kwargs)
|
||||
self.assertIsInstance(kwargs["data"], bytes)
|
||||
self.assertIn(b'"a": 1', kwargs["data"])
|
||||
self.assertIn(b'"b": 2', kwargs["data"])
|
||||
setup_hook_prepare_kwargs(request)
|
||||
self.assertIsInstance(request["data"], bytes)
|
||||
self.assertIn(b'"a": 1', request["data"])
|
||||
self.assertIn(b'"b": 2', request["data"])
|
||||
|
||||
def test_prepare_kwargs_content_type_application_json_charset_utf8(self):
|
||||
kwargs = {
|
||||
request = {
|
||||
"url": "/path",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json; charset=utf-8"
|
||||
},
|
||||
@@ -64,5 +68,5 @@ class TestHttpClient(ApiServerUnittest):
|
||||
"b": 2
|
||||
}
|
||||
}
|
||||
setup_hook_prepare_kwargs("POST", "/path", kwargs)
|
||||
self.assertIsInstance(kwargs["data"], bytes)
|
||||
setup_hook_prepare_kwargs(request)
|
||||
self.assertIsInstance(request["data"], bytes)
|
||||
|
||||
@@ -231,7 +231,7 @@ class VariableBindsUnittest(ApiServerUnittest):
|
||||
|
||||
def test_exec_content_functions(self):
|
||||
test_runner = runner.Runner()
|
||||
content = "${teardown_hook_sleep_1_secs(1)}"
|
||||
content = "${sleep_N_secs(1)}"
|
||||
start_time = time.time()
|
||||
test_runner.context.eval_content(content)
|
||||
end_time = time.time()
|
||||
|
||||
@@ -60,29 +60,91 @@ class TestRunner(ApiServerUnittest):
|
||||
"sign": "f1219719911caae89ccc301679857ebfda115ca2"
|
||||
}
|
||||
},
|
||||
"extract": [
|
||||
{"token": "content.token"}
|
||||
],
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 205},
|
||||
{"check": "content.token", "comparator": "len_eq", "expect": 19}
|
||||
],
|
||||
"teardown_hooks": ["teardown_hook_sleep_1_secs"]
|
||||
]
|
||||
}
|
||||
|
||||
with self.assertRaises(exception.ValidationError):
|
||||
start_time = time.time()
|
||||
self.test_runner.run_test(test)
|
||||
end_time = time.time()
|
||||
# check if teardown function executed
|
||||
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')
|
||||
|
||||
start_time = time.time()
|
||||
runner = HttpRunner().run(testcase_file_path)
|
||||
end_time = time.time()
|
||||
summary = runner.summary
|
||||
self.assertTrue(summary["success"])
|
||||
self.assertLess(end_time - start_time, 1)
|
||||
|
||||
def test_run_testset_with_teardown_hooks_success(self):
|
||||
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}
|
||||
],
|
||||
"teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"]
|
||||
}
|
||||
config_dict = {
|
||||
"path": os.path.join(os.getcwd(), __file__)
|
||||
}
|
||||
self.test_runner.init_config(config_dict, "testset")
|
||||
|
||||
start_time = time.time()
|
||||
self.test_runner.run_test(test)
|
||||
end_time = time.time()
|
||||
# check if teardown function executed
|
||||
self.assertLess(end_time - start_time, 0.5)
|
||||
|
||||
def test_run_testset_with_teardown_hooks_fail(self):
|
||||
test = {
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token2",
|
||||
"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": 404}
|
||||
],
|
||||
"teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"]
|
||||
}
|
||||
config_dict = {
|
||||
"path": os.path.join(os.getcwd(), __file__)
|
||||
}
|
||||
self.test_runner.init_config(config_dict, "testset")
|
||||
|
||||
start_time = time.time()
|
||||
self.test_runner.run_test(test)
|
||||
end_time = time.time()
|
||||
# check if teardown function executed
|
||||
self.assertGreater(end_time - start_time, 2)
|
||||
|
||||
def test_run_testset_hardcode(self):
|
||||
for testcase_file_path in self.testcase_file_path_list:
|
||||
|
||||
@@ -454,6 +454,10 @@ class TestcaseParserUnittest(unittest.TestCase):
|
||||
testcase.parse_function("func(1, 2, a=3, b=4)"),
|
||||
{'func_name': 'func', 'args': [1, 2], 'kwargs': {'a': 3, 'b': 4}}
|
||||
)
|
||||
self.assertEqual(
|
||||
testcase.parse_function("func($request, 123)"),
|
||||
{'func_name': 'func', 'args': ["$request", 123], 'kwargs': {}}
|
||||
)
|
||||
|
||||
def test_parse_content_with_bindings_variables(self):
|
||||
variables = {
|
||||
|
||||
Reference in New Issue
Block a user