mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 22:44:05 +08:00
test: add test for ResponseObject, #948
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
- fix: missing setup/teardown hooks for referenced testcase
|
- fix: missing setup/teardown hooks for referenced testcase
|
||||||
- fix: compatibility for `black` on Android termux that does not support multiprocessing well
|
- fix: compatibility for `black` on Android termux that does not support multiprocessing well
|
||||||
- fix: mishandling of request header `Content-Length` for GET method
|
- 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**
|
**Changed**
|
||||||
|
|
||||||
|
|||||||
@@ -171,6 +171,9 @@ class ResponseObject(object):
|
|||||||
functions_mapping: FunctionsMapping = None,
|
functions_mapping: FunctionsMapping = None,
|
||||||
) -> NoReturn:
|
) -> NoReturn:
|
||||||
|
|
||||||
|
variables_mapping = variables_mapping or {}
|
||||||
|
functions_mapping = functions_mapping or {}
|
||||||
|
|
||||||
self.validation_results = {}
|
self.validation_results = {}
|
||||||
if not validators:
|
if not validators:
|
||||||
return
|
return
|
||||||
|
|||||||
39
tests/response_test.py
Normal file
39
tests/response_test.py
Normal 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,
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user