Search code examples
pythonmockingnose

Running multiple tests cause interference in nosetests when patching with @mock.patch.object and sometimes also when using `with mock.patch.object`


I have observed a very strange behavior of nosetests when using the @mock.patch.object function:

When I run multiple tests at the same time I get different results than when I run them individually. Specifically, it happens, that the override with @mock.patch.object seems to have no effect, in certain cases when I run multiple nosetests together. When I apply the patch with with, this problem does not occur.

@patch.object(ObjectToOverride,....)
def test_mytest()
   # check the override

When using thewith method to apply the patch, subsequent tests are not affected by previous tests.

def test_mytest()
   with patch.object(ObjectToOverride,....):
   # check the override

Any suggestions what could cause this behavior are appreciated.

When I run multiple tests, the ObjectToOverride will be loaded and used by previous tests. But I don't see why using with or decorator makes a difference whether the object can still be patched after that.

In both cases, I can observe some interference between the tests. How can this be avoided in nosetest?


Solution

  • The problem appears to have been connected to tests that throw errors. After that the patch is not torn down correctly.