Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test helper to explain why lists do not match. #315

Merged
merged 3 commits into from
Jul 23, 2022

Conversation

vingkan
Copy link
Collaborator

@vingkan vingkan commented Jul 23, 2022

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 has score=1 and the other has score=0.

Before

If the test uses a raw assertion:

assert actual == expected

Then the pytest assertion error will show all the attributes of the mismatched list items, which can be hard to read:

Screen Shot 2022-07-22 at 7 37 29 PM

After

Now, if you use this new test helper function:

from pipeline.testing import equal_lists

assert equal_lists(actual, expected)

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:

Screen Shot 2022-07-22 at 7 37 00 PM

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:

  1. Lists are equal -> PASS
  2. Lists have different sizes -> FAIL with descriptive error message showing diff between lists
  3. Lists have the same size and same items, but different orders -> FAIL with descriptive error message showing diff between first mismatched pair of items
  4. Lists have the same size, but different items -> FAIL with descriptive error message showing diff between first mismatched pair of items

Caveats

  • If you want to use equal_list you need to import it into your test.
  • Your list values must be sortable to use equal_list, otherwise it will raise an error when checking if the lists have the same values, but different orders.
    • You can set @dataclass(order=True) on a dataclass schema to enable this.
  • This pull request extends pytest assertion rewriting into the pipeline.testing.assertions module so that equal_lists can pick up the pytest assertion message.
    • The pytest assertion message can change at runtime based on the pytest settings, so I have mocked it out of the unit tests for equal_lists.

@vingkan vingkan added pipeline Related to the offline pipelines infra Infrastructure to support features testing Adds a test or enhances testing labels Jul 23, 2022
@gitpod-io
Copy link

gitpod-io bot commented Jul 23, 2022

@ghost
Copy link

ghost commented Jul 23, 2022

👇 Click on the image for a new way to code review
  • Make big changes easier — review code in small groups of related files

  • Know where to start — see the whole change at a glance

  • Take a code tour — explore the change with an interactive tour

  • Make comments and review — all fully sync’ed with github

    Try it now!

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend

@vingkan vingkan merged commit 1767b78 into main Jul 23, 2022
@vingkan vingkan deleted the vk_assert_equal_dataclass_lists branch July 23, 2022 22:45
notedwin pushed a commit that referenced this pull request Jul 29, 2022
* Add test helper to explain why lists do not match.

* Fix default footnote.

* Update comments for each case.
lwilliams717 pushed a commit that referenced this pull request Aug 9, 2022
* Add test helper to explain why lists do not match.

* Fix default footnote.

* Update comments for each case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
infra Infrastructure to support features pipeline Related to the offline pipelines testing Adds a test or enhances testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant