-
Potential pytest bug (see also Comments section). Problem descriptionMy goal is to patch a class' Each of the two tests
Codeimport pytest
# The class to test:
class A:
def __init__(self, x):
self.x = x
def test_1(mocker):
"""First test patches the new method."""
# Create object:
a = A(x=3)
# Make sure new method always returns this object:
mocker.patch.object(A, "__new__", return_value=a)
# Switch off the initializer (so it doesn't overwrite values in the pre-made object):
mocker.patch.object(A, "__init__", return_value=None)
# Do the test:
res = A(x=1)
assert res.x == 3
def test_2():
"""Second test uses the class as intended."""
A(2) # <- Fails (when run right after test_1) CommentsI'm not sure if this is really a pytest issue, since the problem also occurs even if I store away the original Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 3 replies
-
At first glance it looks like misuse of mocking, nothing pytest can do here |
Beta Was this translation helpful? Give feedback.
-
Why is that a misuse? Any idea how I can achieve my goal in "legal" ways? |
Beta Was this translation helpful? Give feedback.
-
BTW, it works if I change the class definition to this: class A(object):
def __new__(cls, *_, **__):
return super(A, cls).__new__(cls)
def __init__(self, x):
self.x = x |
Beta Was this translation helpful? Give feedback.
-
To figure the correct way I'd have to experiment, i would suggest to Patch |
Beta Was this translation helpful? Give feedback.
-
Weird, I can't reproduce the problem either on I would try @RonnyPfannschmidt's suggestion though. |
Beta Was this translation helpful? Give feedback.
-
@ymyke ~4 years later...
Also, see the implementations of I suspect that once the |
Beta Was this translation helpful? Give feedback.
BTW, it works if I change the class definition to this: