Add test helper to explain why lists do not match. #315
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While pairing with @emmadiamon, we realized the error messages for lists of dataclasses can be really hard to read because of the large number of attributes.
Consider the example where a test is compared the actual and expected lists of
Match
objects and the only difference is that one hasscore=1
and the other hasscore=0
.Before
If the test uses a raw assertion:
Then the pytest assertion error will show all the attributes of the mismatched list items, which can be hard to read:
After
Now, if you use this new test helper function:
Then the assertion error will provide a brief explanation and display the attributes that do not match, for the first pair of items that do not match:
This takes advantage of the rich attribute comparison for dataclasses that pytest already supports, but it only works when comparing individual dataclass objects, not a list of dataclass objects: pytest-dev/pytest#3776
Logic
The
equal_lists
helper considers four cases:Caveats
equal_list
you need to import it into your test.equal_list
, otherwise it will raise an error when checking if the lists have the same values, but different orders.@dataclass(order=True)
on a dataclass schema to enable this.pipeline.testing.assertions
module so thatequal_lists
can pick up the pytest assertion message.equal_lists
.