mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
add TeardownHooksFailure exception
This commit is contained in:
@@ -15,6 +15,12 @@ class ValidationFailure(MyBaseFailure):
|
||||
class ExtractFailure(MyBaseFailure):
|
||||
pass
|
||||
|
||||
class SetupHooksFailure(MyBaseFailure):
|
||||
pass
|
||||
|
||||
class TeardownHooksFailure(MyBaseFailure):
|
||||
pass
|
||||
|
||||
|
||||
""" error type exceptions
|
||||
these exceptions will mark test as error
|
||||
|
||||
@@ -153,7 +153,7 @@ class ResponseObject(object):
|
||||
logger.log_error(err_msg)
|
||||
raise exceptions.ExtractFailure(err_msg)
|
||||
|
||||
# new set response attributes
|
||||
# new set response attributes in teardown_hooks
|
||||
elif top_query == "attributes":
|
||||
if not sub_query:
|
||||
# extract response attributes
|
||||
@@ -163,10 +163,10 @@ class ResponseObject(object):
|
||||
return self.attributes[sub_query]
|
||||
else:
|
||||
# content = "attributes.new_attribute_not_exist"
|
||||
err_msg = u"Failed to extract cumstom set attribute! => {}\n".format(field)
|
||||
err_msg = u"Failed to extract cumstom set attribute from teardown hooks! => {}\n".format(field)
|
||||
err_msg += u"response set attributes: {}\n".format(self.attributes)
|
||||
logger.log_error(err_msg)
|
||||
raise exceptions.ExtractFailure(err_msg)
|
||||
raise exceptions.TeardownHooksFailure(err_msg)
|
||||
|
||||
# others
|
||||
else:
|
||||
|
||||
@@ -171,6 +171,7 @@ class Runner(object):
|
||||
# teardown hooks
|
||||
teardown_hooks = testcase_dict.get("teardown_hooks", [])
|
||||
if teardown_hooks:
|
||||
logger.log_info("start to run teardown hooks")
|
||||
self.context.bind_testcase_variable("response", resp_obj)
|
||||
self.do_hook_actions(teardown_hooks)
|
||||
|
||||
|
||||
@@ -103,3 +103,7 @@ def alter_response(response):
|
||||
response.headers["Content-Type"] = "html/text"
|
||||
response.json["headers"]["Host"] = "127.0.0.1:8888"
|
||||
response.attributes["new_attribute"] = "new_attribute"
|
||||
|
||||
def alter_response_error(response):
|
||||
# NameError
|
||||
not_defined_variable
|
||||
|
||||
@@ -222,11 +222,37 @@ class TestRunner(ApiServerUnittest):
|
||||
]
|
||||
}
|
||||
]
|
||||
# with self.assertRaises(exceptions.AssertionError):
|
||||
runner = HttpRunner().run(testsets)
|
||||
|
||||
summary = runner.summary
|
||||
self.assertFalse(summary["success"])
|
||||
self.assertEqual(summary["stat"]["failures"], 1)
|
||||
|
||||
def test_run_httprunner_with_teardown_hooks_error(self):
|
||||
testsets = [
|
||||
{
|
||||
"name": "test teardown hooks",
|
||||
"config": {
|
||||
'path': 'tests/httpbin/hooks.yml',
|
||||
},
|
||||
"testcases": [
|
||||
{
|
||||
"name": "test teardown hooks",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:3458/headers",
|
||||
"method": "GET",
|
||||
"data": "abc"
|
||||
},
|
||||
"teardown_hooks": [
|
||||
"${alter_response_error($response)}"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
runner = HttpRunner().run(testsets)
|
||||
summary = runner.summary
|
||||
self.assertFalse(summary["success"])
|
||||
self.assertEqual(summary["stat"]["errors"], 1)
|
||||
|
||||
def test_run_testset_with_teardown_hooks_success(self):
|
||||
test = {
|
||||
|
||||
Reference in New Issue
Block a user