Testing value in nested list
See original GitHub issueHello, I really enjoy your testing framework. I am relatively new to API writing and was excited to implement tests after only a short time reading your tavern examples. I got a problem though testing for a specific value in a nested dictionary(list(dictionary)) my API returns a large json object (~5k lines) of the structure
{"data":[{"key1":1,"key12":"STRING","key13":{"key131":null,..}, ...},{"key2":2, "key22":"STRING", ... }] "metadata":{"mkey1":"val1"...}
Now I wanted to test if the value of key12
contrains the "STRING"
or not.
so I wrote the test as I already did for other simpler API outputs. I just gave the key structure and exprected it to work as for the simple key:value
test you described in the introductory examples.
response:
status_code: 200
body:
data:
-
key12: "STRING"
but sadly i get an error as the response with the notification that the returned dict does not match the one specified by me
the error message says
expected["data"] = '[{'key12': 'STRING'}]' (type = <class 'list'>), actual["data"] = '[{'key1': 1, 'key12': 'STRING',"key13":{"key131":null, ...
So the test does not check only the key12
value as I expected, but it checked the whole dict described in the body
of response
matches the whole dict returned by the api.
This was quite suprising to me since I already wrote other tests where the api returned a simple dictionary with key:value pairs and I selected a random key and checked for the value and it all worked fine but with the more complex data structure the test is now comparing the literal dictionary setup and not only the value of my key12 …
If this question is already answered somewehre I’m sorry for asking it again but I couldn’t find it.
I also tried to write an external function that processes the returned dictionary and asseses the value of key12
but I was not able to get the connection with pytest yet.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:6 (2 by maintainers)
Did you find any solution on this?
I just raised a similar issue.
Just going to close this to keep discussion in one issue https://github.com/taverntesting/tavern/issues/133#issuecomment-481774309
For the time being you can try using this branch https://github.com/taverntesting/tavern/pull/162 which I am keeping up to date with the master branch