-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Monkeypatch is changing functions context #2860
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
Comments
Hmm that's strange, I cannot reproduce this on the latest My guess is that somehow your fixture is not being executed, but it should. Could you please change it to: @pytest.fixture(autouse=True)
def change_f(monkeypatch):
assert 0
monkeypatch.setattr(my_code, "f", test_1.new_f) And see if that |
Replaced the Just an observation, I used another environment to test now.
|
Upgraded pytest to 3.2.3 and got the same result
When removing the
|
Strange. You can remove the Could you please try your example in a clean virtualenv with only Also, after |
|
Are you sure you used the same file structure as I said? It only happens if it's like this:
|
I could reproduce it, thanks (not sure why I wasn't able before, even with the same file structure). I can see that the def new_f(v):
print('new_f:', Variables.__module__, id(Variables))
...
def test_1():
my_code.f(10)
print('test_1', Variables.__module__, id(Variables))
...
We see that we have the When pytest finds the When pytest imports the Both modules have their own A quick workaround is to add a This is all confusing I admit, and the underlying problem is that pytest does not currently support implicit namespace packages unfortunately. Implicit namespace packages is a complicated topic and a lot of tools ( |
Well, that's sad |
Thanks for the follow up @GabrielSalla |
Uh oh!
There was an error while loading. Please reload this page.
Description
I'm trying to use monkeypath to set a different function from the original code while testing. Not sure if is really a bug or just something intended, but didn't find anything in the documentation.
Monkeypatching works perfectly until I try to implement a way to assert the function was called. When I created a variable to store some flags I would use in the tests (like the class Variables in the following example), I got assertion errors just as they were not set by the function.
Added some prints and running pytest it showed different name for the objects and different ids:
The first line is the print of the
f()
function of themy_code.py
file after the monkeypatch, while the second one is from thetest_1.py
file. I was expecting it to be exactly the same object but it changed it's context and isn't anymore.When changing the
test_1.py
to the same directory asconftest.py
it works again.Pip freeze
Example code
The text was updated successfully, but these errors were encountered: