From d5457c040a502b3997fc8f365ba682df731cec0f Mon Sep 17 00:00:00 2001 From: fengyu Date: Tue, 5 Nov 2019 14:12:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix=20teardown=5Fhook=E5=AF=B9response?= =?UTF-8?q?=E7=9A=84=E4=BF=AE=E6=94=B9=E4=BD=93=E7=8E=B0=E5=9C=A8=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=8A=A5=E5=91=8A=E4=B8=8A=20httprunner#430?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- httprunner/client.py | 14 ++++++++++++-- httprunner/runner.py | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/httprunner/client.py b/httprunner/client.py index bc7cd07e..0a2af89e 100644 --- a/httprunner/client.py +++ b/httprunner/client.py @@ -8,7 +8,7 @@ from requests import Request, Response from requests.exceptions import (InvalidSchema, InvalidURL, MissingSchema, RequestException) -from httprunner import logger +from httprunner import logger, response from httprunner.utils import lower_dict_keys, omit_long_data urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) @@ -115,7 +115,10 @@ class HttpSession(requests.Session): else: try: # try to record json data - req_resp_dict["response"]["json"] = resp_obj.json() + if isinstance(resp_obj, response.ResponseObject): + req_resp_dict["response"]["json"] = resp_obj.json + else: + req_resp_dict["response"]["json"] = resp_obj.json() except ValueError: # only record at most 512 text charactors resp_text = resp_obj.text @@ -126,6 +129,13 @@ class HttpSession(requests.Session): 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): """ Constructs and sends a :py:class:`requests.Request`. diff --git a/httprunner/runner.py b/httprunner/runner.py index 7fe68b4f..13f0a9c9 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -260,6 +260,7 @@ class Runner(object): if teardown_hooks: self.session_context.update_test_variables("response", resp_obj) self.do_hook_actions(teardown_hooks, "teardown") + self.http_client_session.update_last_req_resp_record(resp_obj) # extract extractors = test_dict.get("extract", {}) From d513713ecfcaa6819ccc9ebae8bee29e62f28052 Mon Sep 17 00:00:00 2001 From: fengyu Date: Tue, 5 Nov 2019 19:08:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0#430=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B5=8B=E8=AF=95=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/debugtalk.py | 9 +++++++ .../api/302_redirect_teardown_hook.yml | 13 ++++++++++ tests/test_api.py | 25 ++++++++++++++++--- 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 tests/httpbin/api/302_redirect_teardown_hook.yml diff --git a/tests/debugtalk.py b/tests/debugtalk.py index 7006ddf8..101d6308 100644 --- a/tests/debugtalk.py +++ b/tests/debugtalk.py @@ -127,6 +127,15 @@ def alter_response(response): "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): # NameError diff --git a/tests/httpbin/api/302_redirect_teardown_hook.yml b/tests/httpbin/api/302_redirect_teardown_hook.yml new file mode 100644 index 00000000..329d6d48 --- /dev/null +++ b/tests/httpbin/api/302_redirect_teardown_hook.yml @@ -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"] \ No newline at end of file diff --git a/tests/test_api.py b/tests/test_api.py index 97c59422..d4f2e125 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -419,6 +419,19 @@ class TestHttpRunner(ApiServerUnittest): self.assertEqual(req_resp_data[0]["response"]["status_code"], 302) 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): path = "tests/httpbin/api/302_redirect.yml" summary = self.runner.run(path) @@ -437,13 +450,17 @@ class TestHttpRunner(ApiServerUnittest): def test_run_api_folder(self): api_folder = "tests/httpbin/api/" summary = self.runner.run(api_folder) + print(summary["stat"]["testcases"]["total"]) + print(len(summary["details"])) self.assertTrue(summary["success"]) - self.assertEqual(summary["stat"]["testcases"]["total"], 2) - self.assertEqual(summary["stat"]["teststeps"]["total"], 2) - self.assertEqual(summary["stat"]["teststeps"]["successes"], 2) - self.assertEqual(len(summary["details"]), 2) + self.assertEqual(summary["stat"]["testcases"]["total"], 3) + self.assertEqual(summary["stat"]["teststeps"]["total"], 3) + self.assertEqual(summary["stat"]["teststeps"]["successes"], 3) + self.assertEqual(len(summary["details"]), 3) self.assertEqual(summary["details"][0]["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): for testcase_file_path in self.testcase_file_path_list: