Issue24758
Created on 2015-07-30 19:07 by Randy Syring, last changed 2022-04-11 14:58 by admin. This issue is now closed.
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 12991 | merged | ZackerySpytz, 2019-04-28 05:59 | |
| Messages (7) | |||
|---|---|---|---|
| msg247690 - (view) | Author: Randy Syring (Randy Syring) | Date: 2015-07-30 19:07 | |
In issue http://bugs.python.org/issue21238 a feature was introduced to help prevent false-positive test cases by throwing an AttributeError whenever a non-existent method prefixed with "assert_" is used on a mock object. I had mock objects with legitimate "assert_*" methods that had been working for some time. My tests included calls like: my_mock.assert_in_status.assert_called_once_with(...) My tests started failing unexpectedly and it took me HOURS to figure out that I had a new mock version installed. Those hours could have been turned into seconds my simply giving a better error message, something like: AttributeError: you used "assert_in_status" but that method is not a valid Mock assert method. Please check your spelling. If this was not a typing mistake, you can use the `unsafe` keyword argument to the Mock instance to turn this validation check off. See https://mock-docs...org/ for more details. |
|||
| msg247692 - (view) | Author: Robert Collins (rbcollins) * ![]() |
Date: 2015-07-30 20:00 | |
How were you setting/introducing your assertions on the mocks? e.g. could you supply a small sample script showing what used to work? Thanks. |
|||
| msg247731 - (view) | Author: Randy Syring (Randy Syring) | Date: 2015-07-31 03:35 | |
Old functionality: (temp)rsyring@loftex:~/projects/hllapi-src$ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import mock >>> mock.__version__ '1.0.0' >>> m = mock.Mock() >>> m.assert_screen_status.call_count 0 New functionality: (temp)rsyring@loftex:~/projects/hllapi-src$ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import mock >>> mock.__version__ '1.1.0' >>> m = mock.Mock() >>> m.assert_screen_status.call_count Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/rsyring/.virtualenvs/temp/local/lib/python2.7/site-packages/mock/mock.py", line 714, in __getattr__ raise AttributeError(name) AttributeError: assert_screen_status In my case, the objects I was patching had a legit method like assert_screen_status(). But, after upgrading Mock, the use of those methods started throwing AttributeError's even though it was very obvious that a) the methods existed on the real objects and b) Mock is supposed to let me call anything on I want. As I said previously, this was a VERY confusing situation to figure out. |
|||
| msg248927 - (view) | Author: Robert Collins (rbcollins) * ![]() |
Date: 2015-08-21 00:05 | |
@Randy - ok thanks. So, please do improve the prose in the error message, should be a very straight forward patch. |
|||
| msg341022 - (view) | Author: Zackery Spytz (ZackerySpytz) * ![]() |
Date: 2019-04-28 06:02 | |
I've created a PR for this issue. |
|||
| msg341916 - (view) | Author: Kushal Das (kushal.das) * ![]() |
Date: 2019-05-08 17:32 | |
New changeset b9b08cd948de97d756a199b60becce8397a8c882 by Kushal Das (Zackery Spytz) in branch 'master': bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode (#12991) https://github.com/python/cpython/commit/b9b08cd948de97d756a199b60becce8397a8c882 |
|||
| msg342240 - (view) | Author: Karthikeyan Singaravelan (xtreak) * ![]() |
Date: 2019-05-12 05:21 | |
Closing this as resolved since PR was merged. Thanks Zackery for the PR. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:58:19 | admin | set | github: 68946 |
| 2019-05-12 05:21:13 | xtreak | set | status: open -> closed nosy:
+ xtreak resolution: fixed |
| 2019-05-08 17:32:28 | kushal.das | set | nosy:
+ kushal.das messages: + msg341916 |
| 2019-04-28 06:02:40 | ZackerySpytz | set | nosy:
+ ZackerySpytz messages:
+ msg341022 |
| 2019-04-28 05:59:44 | ZackerySpytz | set | keywords:
+ patch stage: needs patch -> patch review pull_requests: + pull_request12914 |
| 2015-08-21 00:05:20 | rbcollins | set | messages:
+ msg248927 stage: test needed -> needs patch |
| 2015-07-31 03:35:06 | Randy Syring | set | messages: + msg247731 |
| 2015-07-30 20:00:50 | rbcollins | set | stage: test needed |
| 2015-07-30 20:00:40 | rbcollins | set | nosy:
+ rbcollins messages: + msg247692 |
| 2015-07-30 19:11:44 | Dima.Tisnek | set | nosy:
+ Dima.Tisnek |
| 2015-07-30 19:07:05 | Randy Syring | create | |

