I almost went mad by the time I figured out why my test file runs smoothly but breaks if tests are run together for the whole package with pytest.
I have a parent class and a child class. The child calls the parent's init like:
class Child(Parent):
__init__(...,**kwargs):
...
super().__init__(**kwargs)
I wanted to define this behavior with a tiny test.
from unittest.mock import Mock
def test_init_calls_parent_init():
Parent.__init__ = Mock()
Child()
assert Parent.__init__.called
The problem is Parent.__init__ remained persistently a mock for all the following tests in other files.
I had the notion that putting it into a function scope makes it only a temporary change. Of course since those tests broke, they implicitly define the need for the parent init but I wanted to make sure with one explicit test as well.
Should I create some pytest setup/teardown or what is the accepted way to prevent this?