Skip to content

bpo-35047: Better error messages in unittest.mock#10090

Merged
vstinner merged 6 commits into
python:masterfrom
PetterS:bpo-35047
Oct 28, 2018
Merged

bpo-35047: Better error messages in unittest.mock#10090
vstinner merged 6 commits into
python:masterfrom
PetterS:bpo-35047

Conversation

@PetterS

@PetterS PetterS commented Oct 25, 2018

Copy link
Copy Markdown
Contributor

When developing unit tests with unittest.mock, it is common to see error messages like this:

AssertionError: Expected 'info' to not have been called. Called 3 times.

This commit adds the actual list of calls to the error message.

https://bugs.python.org/issue35047

@PetterS

PetterS commented Oct 25, 2018

Copy link
Copy Markdown
Contributor Author

@eamanu here is a PR for the issue I filed.

@PetterS PetterS changed the title bpo-34547: Better error messages in unittest.mock bpo-35047: Better error messages in unittest.mock Oct 25, 2018
When developing unit tests with `unittest.mock`, it is common to see error messages like this:

    AssertionError: Expected 'info' to not have been called. Called 3 times.

This commit adds the actual list of calls to the error message.

@eamanu eamanu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it's ok for me.

@eamanu

eamanu commented Oct 25, 2018

Copy link
Copy Markdown
Contributor

@PetterS look the travis error

@PetterS

PetterS commented Oct 25, 2018

Copy link
Copy Markdown
Contributor Author

The Travis error is that an extra string is printed in the documentation. That string only appears here: https://github.com/python/cpython/blame/e42b705188271da108de42b55d9344642170aa2b/Doc/library/weakref.rst#L493
It is not clear to me if/why my change has anything to do with that.

@PetterS

PetterS commented Oct 25, 2018

Copy link
Copy Markdown
Contributor Author

@vstinner, you seem to have been involved in several PRs involving mock. Do you think you could take a look if time allows?

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about other methods like assert_called_once_with()?

If you use safe_repr(), please modify other exceptions including mock_calls, like assert_has_calls().

cc @mariocj89

Comment thread Lib/unittest/mock.py Outdated
Comment thread Lib/unittest/mock.py Outdated
@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@mariocj89 mariocj89 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note: The link in the PR description does not match the one in the title.

Also, there are no tests validating this change :)

Comment thread Lib/unittest/mock.py Outdated
Comment thread Lib/unittest/mock.py Outdated
@pablogsal

Copy link
Copy Markdown
Member

Notice some examples in library/unittest.mock-examples.rst need to be modified as well to fix the doctests.

@PetterS

PetterS commented Oct 27, 2018

Copy link
Copy Markdown
Contributor Author

@pablogsal This PR modified assert_called_once and assert_not_called, neither of which appears in library/unittest.mock-examples.rst. So I am not sure which changes to make. Anyway, I'll make some requested changes so perhaps it will become clear

Comment thread Lib/unittest/test/testmock/testmock.py Outdated
@PetterS

PetterS commented Oct 27, 2018

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

@bedevere-bot

Copy link
Copy Markdown

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

Comment thread Lib/unittest/mock.py Outdated
Comment thread Lib/unittest/mock.py Outdated
Comment thread Lib/unittest/test/testmock/testmock.py Outdated
Comment thread Misc/NEWS.d/next/Library/2018-10-25-09-59-00.bpo-35047.abbaa.rst Outdated
@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

- Rename helper function. Add optional argument.
- Use re.compile.
- Change NEWS entry.
@PetterS

PetterS commented Oct 28, 2018

Copy link
Copy Markdown
Contributor Author

Thanks for the review! All valid points. I have made the requested changes; please review again.

@bedevere-bot

Copy link
Copy Markdown

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

@vstinner

Copy link
Copy Markdown
Member

@mariocj89: Would you mind to double check the PR, please?

@@ -0,0 +1,3 @@
``unittest.mock`` now includes mock calls in exception messages if

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nitpick: the change affects also assert_called_once_with and assert_has_calls

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

assert_has_calls was only refactored. Is it OK to write assert_called_once(_with)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good point!

About assert_called_once_with, I'd just list the three

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

@mariocj89 mariocj89 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thanks for the patience going through the review :)

@vstinner
vstinner merged commit 47d9424 into python:master Oct 28, 2018
@vstinner

Copy link
Copy Markdown
Member

Thanks for the patience going through the review :)

I concur :-) But the final change is much better than the first version, so it was worth it ;-)

If some people complain that the truncated "Calls: ..." is too short, maybe we can discuss the length later. But right now, let's see how users like safe_repr() default length ;-)

@PetterS

PetterS commented Oct 28, 2018

Copy link
Copy Markdown
Contributor Author

Thanks everyone! I am very pleased with the process of contributing to CPython. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants