I have a class
TestCase where all the tests, except one, need to do the same patch of an object. I'm using Python Mock , and I did the following:
@mock.patch('metodo_alvo', mock.Mock(return_value=1)) class Tests(TestCase): @mock.patch('metodo_alvo', mock.Mock(return_value=2)) def test_override(self): (....)
The idea is that the decorator in
test_override "overwrites" the patch made in the class, but it is not working. When I run the tests,
metodo_alvo gets the class patch.
After much debugging, I discovered that when python builds the test suite, the decorator in
test_override (method) is called before decorator in
Tests (the class) , and as
mock applies patches in that order, the class decorator overrides the behavior of the method decorator.
Is that right? I expected otherwise, and now I'm not sure how to override a patch in the class. Maybe use