question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing value in nested list

See original GitHub issue

Hello, 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
varayalcommented, Feb 11, 2019

Did you find any solution on this?

I just raised a similar issue.

0reactions
michaelboultoncommented, Apr 20, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python | Test for nested list - GeeksforGeeks
The any() is used to check for each of the occurrences and the isinstance() is used to check for the list.
Read more >
How to check if an element is in a nested list? - Stack Overflow
I am trying to define a function nested(x, ys) that tests if a value x appears inside of a nested list of integers...
Read more >
How To Check If a List Is Nested In Python? - Finxter
Method 1: Using isinstance() With any()​​ methods that will help us to check the list if it is nested or not. What is...
Read more >
Nested Lists and Nested Loops
Suppose our list represents repeated heart-rate measurements all for the same patient over a number of tests. Each inner-list is a test/situation and...
Read more >
11. Lists — How to Think Like a Computer Scientist
11.1. List values¶ ... A list within another list is said to be nested. Finally, a list with no elements is called an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found