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

monkeypatch does not work on already-imported function #603

Closed
pytestbot opened this issue Sep 30, 2014 · 4 comments
Closed

monkeypatch does not work on already-imported function #603

pytestbot opened this issue Sep 30, 2014 · 4 comments
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Marco Chomut (BitBucket: Pewpewarrows, GitHub: Pewpewarrows)


Given the following tests file:

#!python

from project.main import foo

def test_foo(monkeypatch):
    monkeypatch.setattr('project.bar.zoo', lambda x: 0)
    assert foo() == 0

And this system under test:

#!python

# project/main.py

from project.bar import zoo

def foo():
    return zoo('lion')

#!python

# project/bar.py

def zoo(animal):
    if animal == 'lion':
        return 7

Then the zoo that's already been imported in main.py remains unpatched. Moving the imports around to be local to the test function would fix the problem, unless main.py is imported by other files at any point during the test suite running. Alternatively, if main.py instead does import project.bar and calls its dependency with project.bar.zoo('lion'), then the test passes again. But this is cumbersome and difficult to enforce across an entire project.


@pytestbot
Copy link
Contributor Author

Original comment by Anatoly Bubenkov (BitBucket: bubenkoff, GitHub: bubenkoff):


monkeypatch receives the object, not the dotted name of the object to mock
so:

#!python

from project import bar

def test_foo(monkeypatch):
    monkeypatch.setattr(bar, 'zoo', lambda x: 0)
    assert foo() == 0

@pytestbot
Copy link
Contributor Author

Original comment by Marco Chomut (BitBucket: Pewpewarrows, GitHub: Pewpewarrows):


The documentation disagrees:

"For convenience you can specify a string as target which will be interpreted as a dotted import path, with the last part being the attribute name."

http://pytest.org/latest/monkeypatch.html

@pytestbot
Copy link
Contributor Author

Original comment by Anatoly Bubenkov (BitBucket: bubenkoff, GitHub: bubenkoff):


oops, sorry, didn't know we support also that :)
but in your case there's another issue, you patch project.bar.zoo
but you import zoo into the main, so you should either import bar instead of zoo, or monkey patch the zoo in the main

@pytestbot
Copy link
Contributor Author

Original comment by Marco Chomut (BitBucket: Pewpewarrows, GitHub: Pewpewarrows):


Oh interesting, and that makes sense now that you pointed it out. If I change the patching line to:

#!python

monkeypatch.setattr('project.main.zoo', lambda x: 0)

then everything works as intended. It seems that monkeypatch is working as intended then, sorry for the ticket. I suppose some documentation on this particular piece of behavior might be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

1 participant