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

Prefer actual-expected to expected-actual in assertions #5129

Merged

Conversation

radoering
Copy link
Member

Relates-to: #4776

As far as I know, there is no general convention about the order (actual-expected vs. expected-actual) in pytest assertions. However, I think a consistent order is desirable in order to quickly grasp what is going wrong in a failing test.

In my opinion, there is at least one strong argument for actual-expected:
SIM300 of flake8-simplify is about avoiding yoda conditions. Thus, age == 42 is preferred to 42 == age. Avoiding yoda conditions often results in actual-expected order.

Further, most examples in pytest documentation are in actual-expected order, e.g. https://docs.pytest.org/en/6.2.x/index.html. (The same applies to unittest: https://docs.python.org/3/library/unittest.html) Probably, actual-expected is the more natural or at least more common order.

I am not aware if there is a linter that can help to keep actual-expected order because sometimes the context is required in order to decide which is actual and which is expected. At least, if most of the tests use a consistent order it will be more likely that this order is used in new tests.

Copy link
Member

@finswimmer finswimmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@finswimmer finswimmer merged commit b99b455 into python-poetry:master Feb 3, 2022
@radoering radoering mentioned this pull request Apr 14, 2022
2 tasks
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
@radoering radoering deleted the assert-actual-expected-order branch November 24, 2024 12:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants