From 5673c0ca925c53aef8598f92b2f7b86924069c7c Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 30 Jun 2020 15:21:50 +0800 Subject: [PATCH] fix: validate with variable or function whose evaluation result is "" or not text --- docs/CHANGELOG.md | 6 ++++++ httprunner/response.py | 6 +++++- tests/response_test.py | 18 +++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 5bd0fc5e..8ef6c42f 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,11 @@ # Release History +## 3.1.3 (2020-06-30) + +**Fixed** + +- fix: validate with variable or function whose evaluation result is "" or not text + ## 3.1.2 (2020-06-29) **Fixed** diff --git a/httprunner/response.py b/httprunner/response.py index 6b129a97..ea893166 100644 --- a/httprunner/response.py +++ b/httprunner/response.py @@ -197,7 +197,11 @@ class ResponseObject(object): ) check_item = parse_string_value(check_item) - check_value = jmespath.search(check_item, self.resp_obj_meta) + if check_item and isinstance(check_item, Text): + check_value = jmespath.search(check_item, self.resp_obj_meta) + else: + # variable or function evaluation result is "" or not text + check_value = check_item # comparator assert_method = u_validator["assert"] diff --git a/tests/response_test.py b/tests/response_test.py index f6f8e936..7f98cfa3 100644 --- a/tests/response_test.py +++ b/tests/response_test.py @@ -28,12 +28,28 @@ class TestResponse(unittest.TestCase): self.assertEqual(extract_mapping["var_2"], "Olympia") def test_validate(self): - variables_mapping = {"index": 1} self.resp_obj.validate( [ {"eq": ["body.json.locations[0].name", "Seattle"]}, {"eq": ["body.json.locations[0]", {"name": "Seattle", "state": "WA"}]}, + ], + ) + + def test_validate_variables(self): + variables_mapping = {"index": 1, "var_empty": ""} + self.resp_obj.validate( + [ {"eq": ["body.json.locations[$index].name", "New York"]}, + {"eq": ["$var_empty", ""]}, ], variables_mapping=variables_mapping, ) + + def test_validate_functions(self): + variables_mapping = {"index": 1} + functions_mapping = {"get_num": lambda x: x} + self.resp_obj.validate( + [{"eq": ["${get_num(0)}", 0]}, {"eq": ["${get_num($index)}", 1]},], + variables_mapping=variables_mapping, + functions_mapping=functions_mapping, + )