test: add test for ResponseObject, #948

This commit is contained in:
debugtalk
2020-06-29 19:08:39 +08:00
parent 880a4e9e4a
commit 62f0544aaf
3 changed files with 43 additions and 0 deletions

View File

@@ -7,6 +7,7 @@
- fix: missing setup/teardown hooks for referenced testcase
- fix: compatibility for `black` on Android termux that does not support multiprocessing well
- fix: mishandling of request header `Content-Length` for GET method
- fix: validate with jmespath containing variable or function, e.g. `body.locations[$index].name`
**Changed**

View File

@@ -171,6 +171,9 @@ class ResponseObject(object):
functions_mapping: FunctionsMapping = None,
) -> NoReturn:
variables_mapping = variables_mapping or {}
functions_mapping = functions_mapping or {}
self.validation_results = {}
if not validators:
return

39
tests/response_test.py Normal file
View File

@@ -0,0 +1,39 @@
import unittest
import requests
from httprunner.response import ResponseObject
class TestResponse(unittest.TestCase):
def setUp(self) -> None:
resp = requests.post(
"https://httpbin.org/anything",
json={
"locations": [
{"name": "Seattle", "state": "WA"},
{"name": "New York", "state": "NY"},
{"name": "Bellevue", "state": "WA"},
{"name": "Olympia", "state": "WA"},
]
},
)
self.resp_obj = ResponseObject(resp)
def test_extract(self):
extract_mapping = self.resp_obj.extract(
{"var_1": "body.json.locations[0]", "var_2": "body.json.locations[3].name"}
)
self.assertEqual(extract_mapping["var_1"], {"name": "Seattle", "state": "WA"})
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"}]},
{"eq": ["body.json.locations[$index].name", "New York"]},
],
variables_mapping=variables_mapping,
)