More descriptive behaviour when using assert in with array of dictionaries

We recently had a unit test where we needed to track if an item was in an array of dicts, we currently use this to verify we sent a specific analytics event by inspecting an event queue represented by an array:

assert {"name": "Hello World" } in events

This works well for simple types or object types which may implement more advanced way of describing how they are different, but with dictionaries we just get a large wall of text i.e “{} was not in array [{},{}…” (Text shortened for brevity)

In my ideal world we would use the hamming distance of the items in the object to determine which item was most likely to be the item we are after but for whatever reason has something different about it.

For example the text could have the full explanation as it does now but below it could have something like this

“It looks like {“name”: “Hello Worlg” } is the item you are looking for” and then do the same output as when you do assert {{"name": "Hello World" } == {"name": "Hello Worlg" }

This is not a feature request for python; python does not generate such message from assert statements. Instead you need to make this request to pytest which actually does this.

2 Likes