mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
extract meta data of request and bind it to testcase
This commit is contained in:
@@ -122,11 +122,11 @@ class HttpSession(requests.Session):
|
||||
logger.log_info("{method} {url}".format(method=method, url=url))
|
||||
logger.log_debug("request kwargs(raw): {kwargs}".format(kwargs=kwargs))
|
||||
# store meta data that is used when reporting the request to locust's statistics
|
||||
request_meta = {}
|
||||
meta_data = {}
|
||||
|
||||
# set up pre_request hook for attaching meta data to the request object
|
||||
request_meta["method"] = method
|
||||
request_meta["start_time"] = time.time()
|
||||
meta_data["method"] = method
|
||||
meta_data["request_time"] = time.time()
|
||||
|
||||
if "httpntlmauth" in kwargs:
|
||||
from requests_ntlm import HttpNtlmAuth
|
||||
@@ -137,24 +137,24 @@ class HttpSession(requests.Session):
|
||||
kwargs.setdefault("timeout", 120)
|
||||
|
||||
response = self._send_request_safe_mode(method, url, **kwargs)
|
||||
request_meta["url"] = (response.history and response.history[0] or response)\
|
||||
meta_data["url"] = (response.history and response.history[0] or response)\
|
||||
.request.path_url
|
||||
|
||||
# record the consumed time
|
||||
request_meta["response_time"] = int((time.time() - request_meta["start_time"]) * 1000)
|
||||
meta_data["response_time"] = int((time.time() - meta_data["request_time"]) * 1000)
|
||||
|
||||
# get the length of the content, but if the argument stream is set to True, we take
|
||||
# the size from the content-length header, in order to not trigger fetching of the body
|
||||
if kwargs.get("stream", False):
|
||||
request_meta["content_size"] = int(response.headers.get("content-length") or 0)
|
||||
meta_data["content_size"] = int(response.headers.get("content-length") or 0)
|
||||
else:
|
||||
request_meta["content_size"] = len(response.content or "")
|
||||
meta_data["content_size"] = len(response.content or "")
|
||||
|
||||
request_meta["request_headers"] = response.request.headers
|
||||
request_meta["request_body"] = response.request.body
|
||||
request_meta["status_code"] = response.status_code
|
||||
request_meta["response_headers"] = response.headers
|
||||
request_meta["response_content"] = response.content
|
||||
meta_data["request_headers"] = response.request.headers
|
||||
meta_data["request_body"] = response.request.body
|
||||
meta_data["status_code"] = response.status_code
|
||||
meta_data["response_headers"] = response.headers
|
||||
meta_data["response_body"] = response.text
|
||||
|
||||
logger.log_debug("response status_code: {}".format(response.status_code))
|
||||
logger.log_debug("response headers: {}".format(response.headers))
|
||||
@@ -167,9 +167,10 @@ class HttpSession(requests.Session):
|
||||
else:
|
||||
logger.log_info(
|
||||
"""status_code: {}, response_time: {} ms, response_length: {} bytes"""\
|
||||
.format(request_meta["status_code"], request_meta["response_time"], \
|
||||
request_meta["content_size"]))
|
||||
.format(meta_data["status_code"], meta_data["response_time"], \
|
||||
meta_data["content_size"]))
|
||||
|
||||
self.meta_data = meta_data
|
||||
return response
|
||||
|
||||
def _send_request_safe_mode(self, method, url, **kwargs):
|
||||
|
||||
@@ -173,6 +173,8 @@ class Runner(object):
|
||||
finally:
|
||||
setup_teardown(teardown_actions)
|
||||
|
||||
return getattr(self.http_client_session, "meta_data", {})
|
||||
|
||||
def extract_output(self, output_variables_list):
|
||||
""" extract output variables
|
||||
"""
|
||||
|
||||
@@ -16,7 +16,7 @@ class TestCase(unittest.TestCase):
|
||||
def runTest(self):
|
||||
""" run testcase and check result.
|
||||
"""
|
||||
self.test_runner.run_test(self.testcase_dict)
|
||||
self.meta_data = self.test_runner.run_test(self.testcase_dict)
|
||||
|
||||
class TestSuite(unittest.TestSuite):
|
||||
""" create test suite with a testset, it may include one or several testcases.
|
||||
|
||||
Reference in New Issue
Block a user