-
Notifications
You must be signed in to change notification settings - Fork 1.1k
HACKING.rst: more unit testing documentation #354
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
Merged
+55
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a253d4b
HACKING.rst: include warning against mock assert methods
OddBloke 64f2785
HACKING.rst: add guidelines on test decorator/param ordering
OddBloke 20f743d
HACKING.rst: add test guideline for module-level mock variables
OddBloke d13f470
HACKING.rst: change assert_ method docs to permit use with autospeccing
OddBloke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pylint and most editors should highlight methods not found. If we require linting on our commits, I don't think we should restrict these methods.Python will raise an exception if you create a mock method that starts with "assert" or "assret" that isn't one of the defined ones, so I don't think there's that much risk here. https://github.com/python/cpython/blob/master/Lib/unittest/mock.py#L634
I've used these methods and personally find them a lot clearer than the naked asserts. Could just be bias for what I'm used to though.
Should we favor autospeccing instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback James!
I agree that there isn't a huge amount of risk here, but the fact that changes have been made to Python to surface this issue to users indicates, to me, that's it's a bigger problem than not. It identifies that there is enough of a problem to change
unittest.mocks behaviour, but the change to the library doesn't actually protect against all typos that could be made in these methods.(And, to be clear, I don't think there's any way that you can protect against all typos. The assertions should not be in the mock object's namespace: that's the fundamental design mistake in these methods, IMO.)
That's an important point. For me, I've never used these methods so I simply see them as a code smell. But they do make test code easier to understand for folks who are used to them, and that's valuable.
Perhaps we could change this text to say that we allow the usage of these methods but only on mocks that have a spec/are autospec'd? Does that sound reasonable? (We should also be doing more autospec'ing in general, no doubt.)
I think that the "permissible if autospec'd" is probably our best path forward but, for completeness, an alternative that does allow us to catch all typos would be to lookup the methods on the Mock type and then call them with our mock:
If we really wanted, we could set these up as helper functions (which has been proposed for CPython) somewhere:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems reasonable. It would also encourage me to actually use autospecing as I usually just default to a standard mock anyway.
I think I currently like the autospecing exception without the additional helper functions. If we decide we want them later, we can always add them.