mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-12 16:01:27 +08:00
bugfix: handle when testcase failed
This commit is contained in:
@@ -116,17 +116,17 @@ class HttpSession(requests.Session):
|
|||||||
:param cert: (optional)
|
:param cert: (optional)
|
||||||
if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
|
if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
|
||||||
"""
|
"""
|
||||||
|
# store detail data of request and response
|
||||||
|
self.meta_data = {}
|
||||||
|
|
||||||
# prepend url with hostname unless it's already an absolute URL
|
# prepend url with hostname unless it's already an absolute URL
|
||||||
url = self._build_url(url)
|
url = self._build_url(url)
|
||||||
logger.log_info("{method} {url}".format(method=method, url=url))
|
logger.log_info("{method} {url}".format(method=method, url=url))
|
||||||
logger.log_debug("request kwargs(raw): {kwargs}".format(kwargs=kwargs))
|
logger.log_debug("request kwargs(raw): {kwargs}".format(kwargs=kwargs))
|
||||||
# store meta data that is used when reporting the request to locust's statistics
|
|
||||||
meta_data = {}
|
|
||||||
|
|
||||||
# set up pre_request hook for attaching meta data to the request object
|
# set up pre_request hook for attaching meta data to the request object
|
||||||
meta_data["method"] = method
|
self.meta_data["method"] = method
|
||||||
meta_data["request_time"] = time.time()
|
self.meta_data["request_time"] = time.time()
|
||||||
|
|
||||||
if "httpntlmauth" in kwargs:
|
if "httpntlmauth" in kwargs:
|
||||||
from requests_ntlm import HttpNtlmAuth
|
from requests_ntlm import HttpNtlmAuth
|
||||||
@@ -137,24 +137,24 @@ class HttpSession(requests.Session):
|
|||||||
kwargs.setdefault("timeout", 120)
|
kwargs.setdefault("timeout", 120)
|
||||||
|
|
||||||
response = self._send_request_safe_mode(method, url, **kwargs)
|
response = self._send_request_safe_mode(method, url, **kwargs)
|
||||||
meta_data["url"] = (response.history and response.history[0] or response)\
|
self.meta_data["url"] = (response.history and response.history[0] or response)\
|
||||||
.request.path_url
|
.request.path_url
|
||||||
|
|
||||||
# record the consumed time
|
# record the consumed time
|
||||||
meta_data["response_time"] = int((time.time() - meta_data["request_time"]) * 1000)
|
self.meta_data["response_time"] = int((time.time() - self.meta_data["request_time"]) * 1000)
|
||||||
|
|
||||||
# get the length of the content, but if the argument stream is set to True, we take
|
# 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
|
# the size from the content-length header, in order to not trigger fetching of the body
|
||||||
if kwargs.get("stream", False):
|
if kwargs.get("stream", False):
|
||||||
meta_data["content_size"] = int(response.headers.get("content-length") or 0)
|
self.meta_data["content_size"] = int(response.headers.get("content-length") or 0)
|
||||||
else:
|
else:
|
||||||
meta_data["content_size"] = len(response.content or "")
|
self.meta_data["content_size"] = len(response.content or "")
|
||||||
|
|
||||||
meta_data["request_headers"] = response.request.headers
|
self.meta_data["request_headers"] = response.request.headers
|
||||||
meta_data["request_body"] = response.request.body
|
self.meta_data["request_body"] = response.request.body
|
||||||
meta_data["status_code"] = response.status_code
|
self.meta_data["status_code"] = response.status_code
|
||||||
meta_data["response_headers"] = response.headers
|
self.meta_data["response_headers"] = response.headers
|
||||||
meta_data["response_body"] = response.text
|
self.meta_data["response_body"] = response.text
|
||||||
|
|
||||||
logger.log_debug("response status_code: {}".format(response.status_code))
|
logger.log_debug("response status_code: {}".format(response.status_code))
|
||||||
logger.log_debug("response headers: {}".format(response.headers))
|
logger.log_debug("response headers: {}".format(response.headers))
|
||||||
@@ -167,10 +167,9 @@ class HttpSession(requests.Session):
|
|||||||
else:
|
else:
|
||||||
logger.log_info(
|
logger.log_info(
|
||||||
"""status_code: {}, response_time: {} ms, response_length: {} bytes"""\
|
"""status_code: {}, response_time: {} ms, response_length: {} bytes"""\
|
||||||
.format(meta_data["status_code"], meta_data["response_time"], \
|
.format(self.meta_data["status_code"], self.meta_data["response_time"], \
|
||||||
meta_data["content_size"]))
|
self.meta_data["content_size"]))
|
||||||
|
|
||||||
self.meta_data = meta_data
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def _send_request_safe_mode(self, method, url, **kwargs):
|
def _send_request_safe_mode(self, method, url, **kwargs):
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ class HtmlTestResult(unittest.TextTestResult):
|
|||||||
self.records.append({
|
self.records.append({
|
||||||
'name': test.shortDescription(),
|
'name': test.shortDescription(),
|
||||||
'status': status,
|
'status': status,
|
||||||
'response_time': test.meta_data["response_time"],
|
'response_time': test.meta_data.get("response_time", 0),
|
||||||
'attachment': attachment,
|
'attachment': attachment,
|
||||||
"meta_data": test.meta_data
|
"meta_data": test.meta_data
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class TestCase(unittest.TestCase):
|
|||||||
super(TestCase, self).__init__()
|
super(TestCase, self).__init__()
|
||||||
self.test_runner = test_runner
|
self.test_runner = test_runner
|
||||||
self.testcase_dict = testcase_dict
|
self.testcase_dict = testcase_dict
|
||||||
|
self.meta_data = {}
|
||||||
|
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
""" run testcase and check result.
|
""" run testcase and check result.
|
||||||
|
|||||||
Reference in New Issue
Block a user