diff --git a/httprunner/__about__.py b/httprunner/__about__.py index 995d5ee3..9fc5c801 100644 --- a/httprunner/__about__.py +++ b/httprunner/__about__.py @@ -1,7 +1,7 @@ __title__ = 'HttpRunner' __description__ = 'One-stop solution for HTTP(S) testing.' __url__ = 'https://github.com/HttpRunner/HttpRunner' -__version__ = '1.5.5' +__version__ = '1.5.6' __author__ = 'debugtalk' __author_email__ = 'mail@debugtalk.com' __license__ = 'MIT' diff --git a/httprunner/response.py b/httprunner/response.py index d00a397c..9a2a3b89 100644 --- a/httprunner/response.py +++ b/httprunner/response.py @@ -82,6 +82,16 @@ class ResponseObject(object): err_msg += u"attribute: {}".format(sub_query) logger.log_error(err_msg) raise exception.ParamsError(err_msg) + elif top_query == "elapsed": + if sub_query in ["days", "seconds", "microseconds"]: + return getattr(self.elapsed, sub_query) + elif sub_query == "total_seconds": + return self.elapsed.total_seconds() + else: + err_msg = "{}: {} is not valid timedelta attribute.\n".format(field, sub_query) + err_msg += "elapsed only support attributes: days, seconds, microseconds, total_seconds.\n" + logger.log_error(err_msg) + raise exception.ParamsError(err_msg) try: top_query_content = getattr(self, top_query) @@ -96,11 +106,11 @@ class ResponseObject(object): # TODO: remove compatibility for content, text if isinstance(top_query_content, bytes): top_query_content = top_query_content.decode("utf-8") - + if isinstance(top_query_content, PreparedRequest): top_query_content = top_query_content.__dict__ - else: - top_query_content = json.loads(top_query_content) + else: + top_query_content = json.loads(top_query_content) except json.decoder.JSONDecodeError: err_msg = u"Failed to extract data with delimiter!\n" err_msg += u"response content: {}\n".format(self.content) diff --git a/tests/test_runner.py b/tests/test_runner.py index 460f90c6..390956eb 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -332,3 +332,30 @@ class TestRunner(ApiServerUnittest): self.assertTrue(summary["success"]) self.assertEqual(len(summary["details"][0]["output"]), 3 * 2 * 2) self.assertEqual(summary["stat"]["testsRun"], 3 * 2 * 2) + + def test_run_validate_elapsed(self): + test = { + "name": "get token", + "request": { + "url": "http://127.0.0.1:5000/api/get-token", + "method": "POST", + "headers": { + "content-type": "application/json", + "user_agent": "iOS/10.3", + "device_sn": "HZfFBh6tU59EdXJ", + "os_platform": "ios", + "app_version": "2.8.6" + }, + "json": { + "sign": "f1219719911caae89ccc301679857ebfda115ca2" + } + }, + "validate": [ + {"check": "status_code", "expect": 200}, + {"check": "elapsed.seconds", "comparator": "lt", "expect": 1}, + {"check": "elapsed.days", "comparator": "eq", "expect": 0}, + {"check": "elapsed.microseconds", "comparator": "gt", "expect": 1000}, + {"check": "elapsed.total_seconds", "comparator": "lt", "expect": 1} + ] + } + self.test_runner.run_test(test)