mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 15:37:35 +08:00
Merge pull request #744 from sirfengyu/master
fix: teardown_hook 对 response的修改体现在测试报告上 httprunner #430
This commit is contained in:
+11
-1
@@ -8,7 +8,7 @@ from requests import Request, Response
|
|||||||
from requests.exceptions import (InvalidSchema, InvalidURL, MissingSchema,
|
from requests.exceptions import (InvalidSchema, InvalidURL, MissingSchema,
|
||||||
RequestException)
|
RequestException)
|
||||||
|
|
||||||
from httprunner import logger
|
from httprunner import logger, response
|
||||||
from httprunner.utils import lower_dict_keys, omit_long_data
|
from httprunner.utils import lower_dict_keys, omit_long_data
|
||||||
|
|
||||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||||
@@ -115,6 +115,9 @@ class HttpSession(requests.Session):
|
|||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
# try to record json data
|
# try to record json data
|
||||||
|
if isinstance(resp_obj, response.ResponseObject):
|
||||||
|
req_resp_dict["response"]["json"] = resp_obj.json
|
||||||
|
else:
|
||||||
req_resp_dict["response"]["json"] = resp_obj.json()
|
req_resp_dict["response"]["json"] = resp_obj.json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# only record at most 512 text charactors
|
# only record at most 512 text charactors
|
||||||
@@ -126,6 +129,13 @@ class HttpSession(requests.Session):
|
|||||||
|
|
||||||
return req_resp_dict
|
return req_resp_dict
|
||||||
|
|
||||||
|
def update_last_req_resp_record(self, resp_obj):
|
||||||
|
"""
|
||||||
|
update request and response info from Response() object.
|
||||||
|
"""
|
||||||
|
self.meta_data["data"].pop()
|
||||||
|
self.meta_data["data"].append(self.get_req_resp_record(resp_obj))
|
||||||
|
|
||||||
def request(self, method, url, name=None, **kwargs):
|
def request(self, method, url, name=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
Constructs and sends a :py:class:`requests.Request`.
|
Constructs and sends a :py:class:`requests.Request`.
|
||||||
|
|||||||
@@ -260,6 +260,7 @@ class Runner(object):
|
|||||||
if teardown_hooks:
|
if teardown_hooks:
|
||||||
self.session_context.update_test_variables("response", resp_obj)
|
self.session_context.update_test_variables("response", resp_obj)
|
||||||
self.do_hook_actions(teardown_hooks, "teardown")
|
self.do_hook_actions(teardown_hooks, "teardown")
|
||||||
|
self.http_client_session.update_last_req_resp_record(resp_obj)
|
||||||
|
|
||||||
# extract
|
# extract
|
||||||
extractors = test_dict.get("extract", {})
|
extractors = test_dict.get("extract", {})
|
||||||
|
|||||||
@@ -127,6 +127,15 @@ def alter_response(response):
|
|||||||
"key": 123
|
"key": 123
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def alter_response_302(response):
|
||||||
|
response.status_code = 500
|
||||||
|
response.headers["Content-Type"] = "html/text"
|
||||||
|
response.text = "abcdef"
|
||||||
|
response.new_attribute = "new_attribute_value"
|
||||||
|
response.new_attribute_dict = {
|
||||||
|
"key": 123
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def alter_response_error(response):
|
def alter_response_error(response):
|
||||||
# NameError
|
# NameError
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
name: 302 redirect
|
||||||
|
request:
|
||||||
|
url: https://httpbin.org/redirect-to
|
||||||
|
params:
|
||||||
|
url: https://github.com
|
||||||
|
status_code: 302
|
||||||
|
method: GET
|
||||||
|
verify: False
|
||||||
|
teardown_hooks:
|
||||||
|
- ${alter_response_302($response)}
|
||||||
|
validate:
|
||||||
|
- eq: ["status_code", 500]
|
||||||
|
- eq: ["text","abcdef"]
|
||||||
+21
-4
@@ -419,6 +419,19 @@ class TestHttpRunner(ApiServerUnittest):
|
|||||||
self.assertEqual(req_resp_data[0]["response"]["status_code"], 302)
|
self.assertEqual(req_resp_data[0]["response"]["status_code"], 302)
|
||||||
self.assertEqual(req_resp_data[1]["response"]["status_code"], 200)
|
self.assertEqual(req_resp_data[1]["response"]["status_code"], 200)
|
||||||
|
|
||||||
|
def test_request_302_logs_teardown_hook(self):
|
||||||
|
path = "tests/httpbin/api/302_redirect_teardown_hook.yml"
|
||||||
|
summary = self.runner.run(path)
|
||||||
|
self.assertTrue(summary["success"])
|
||||||
|
self.assertEqual(summary["stat"]["testcases"]["total"], 1)
|
||||||
|
self.assertEqual(summary["stat"]["teststeps"]["total"], 1)
|
||||||
|
self.assertEqual(summary["stat"]["teststeps"]["successes"], 1)
|
||||||
|
|
||||||
|
req_resp_data = summary["details"][0]["records"][0]["meta_datas"]["data"]
|
||||||
|
self.assertEqual(len(req_resp_data), 2)
|
||||||
|
self.assertEqual(req_resp_data[0]["response"]["status_code"], 302)
|
||||||
|
self.assertEqual(req_resp_data[1]["response"]["status_code"], 500)
|
||||||
|
|
||||||
def test_request_with_params(self):
|
def test_request_with_params(self):
|
||||||
path = "tests/httpbin/api/302_redirect.yml"
|
path = "tests/httpbin/api/302_redirect.yml"
|
||||||
summary = self.runner.run(path)
|
summary = self.runner.run(path)
|
||||||
@@ -437,13 +450,17 @@ class TestHttpRunner(ApiServerUnittest):
|
|||||||
def test_run_api_folder(self):
|
def test_run_api_folder(self):
|
||||||
api_folder = "tests/httpbin/api/"
|
api_folder = "tests/httpbin/api/"
|
||||||
summary = self.runner.run(api_folder)
|
summary = self.runner.run(api_folder)
|
||||||
|
print(summary["stat"]["testcases"]["total"])
|
||||||
|
print(len(summary["details"]))
|
||||||
self.assertTrue(summary["success"])
|
self.assertTrue(summary["success"])
|
||||||
self.assertEqual(summary["stat"]["testcases"]["total"], 2)
|
self.assertEqual(summary["stat"]["testcases"]["total"], 3)
|
||||||
self.assertEqual(summary["stat"]["teststeps"]["total"], 2)
|
self.assertEqual(summary["stat"]["teststeps"]["total"], 3)
|
||||||
self.assertEqual(summary["stat"]["teststeps"]["successes"], 2)
|
self.assertEqual(summary["stat"]["teststeps"]["successes"], 3)
|
||||||
self.assertEqual(len(summary["details"]), 2)
|
self.assertEqual(len(summary["details"]), 3)
|
||||||
self.assertEqual(summary["details"][0]["stat"]["total"], 1)
|
self.assertEqual(summary["details"][0]["stat"]["total"], 1)
|
||||||
self.assertEqual(summary["details"][1]["stat"]["total"], 1)
|
self.assertEqual(summary["details"][1]["stat"]["total"], 1)
|
||||||
|
self.assertEqual(summary["details"][2]["stat"]["total"], 1)
|
||||||
|
|
||||||
|
|
||||||
def test_run_testcase_hardcode(self):
|
def test_run_testcase_hardcode(self):
|
||||||
for testcase_file_path in self.testcase_file_path_list:
|
for testcase_file_path in self.testcase_file_path_list:
|
||||||
|
|||||||
Reference in New Issue
Block a user