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

unittest.mock.patch.stopall intermittently doesn't work when the same thing is patched multiple times #65438

Closed
voidspace opened this issue Apr 15, 2014 · 2 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@voidspace
Copy link
Contributor

BPO 21239
Nosy @voidspace

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/voidspace'
closed_at = <Date 2014-04-15.21:22:29.474>
created_at = <Date 2014-04-15.15:54:31.458>
labels = ['type-bug']
title = "unittest.mock.patch.stopall intermittently doesn't work when the same thing is patched multiple times"
updated_at = <Date 2014-04-15.21:22:29.472>
user = 'https://github.com/voidspace'

bugs.python.org fields:

activity = <Date 2014-04-15.21:22:29.472>
actor = 'python-dev'
assignee = 'michael.foord'
closed = True
closed_date = <Date 2014-04-15.21:22:29.474>
closer = 'python-dev'
components = []
creation = <Date 2014-04-15.15:54:31.458>
creator = 'michael.foord'
dependencies = []
files = []
hgrepos = []
issue_num = 21239
keywords = []
message_count = 2.0
messages = ['216322', '216388']
nosy_count = 2.0
nosy_names = ['michael.foord', 'python-dev']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue21239'
versions = ['Python 3.4', 'Python 3.5']

@voidspace
Copy link
Contributor Author

stopall does not always stop all patches to single target
http://code.google.com/p/mock/issues/detail?id=226

What steps will reproduce the problem?

python code to reproduce error

import mock

def myfunc():
   return 'hello'

m = mock.patch('__main__.myfunc').start()
m.return_value = 'firstmock'
myfunc()

m2 = mock.patch('__main__.myfunc').start()
m2.return_value = 'secondmock'
myfunc()

mock.patch.stopall()
myfunc()

What is the expected output? What do you see instead?
I would expect the output from above to be:
'firstmock'
'secondmock'
'hello'

Instead, sometimes it comes out as:
'firstmock'
'secondmock'
'firstmock'

This result is non-deterministic though so it may also come out as expected. Re-run several times to get the error.

This is a result of using a set to store the active patches. Conversion from a set to a list is non-deterministic because the set has no notion of order.

Please provide any additional information below.

This use case may seem strange, but it shows up in large unit tests where a base class sets up a default patch to catch external calls or something similar and then an individual unit test requiring specific mock behavior patches it differently.

I have a patch here that fixes the problem using a list to store the patches to maintain order and call them in reverse order to stop them in the correct order.

https://code.google.com/r/blak111-mockfix/source/detail?r=3c2f72b0253075d628afb333a79b7cb118132294

@voidspace voidspace self-assigned this Apr 15, 2014
@voidspace voidspace added the type-bug An unexpected behavior, bug, or error label Apr 15, 2014
@python-dev
Copy link
Mannequin

python-dev mannequin commented Apr 15, 2014

New changeset 727b7e9c40e3 by Michael Foord in branch '3.4':
Closes bpo-21239. unittest.mock.patch.stopall() did not work deterministically when the same name was patched multiple times.
http://hg.python.org/cpython/rev/727b7e9c40e3

@python-dev python-dev mannequin closed this as completed Apr 15, 2014
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant