I want to mock something for an entire class but the following minimal example is not working:
import time
import six
if six.PY2:
import mock
else:
from unittest import mock
@mock.patch('time.sleep', mock.Mock(side_effect=Exception('dooooom')))
class Foo(object):
def bar(self):
print('before')
time.sleep(1)
print('after')
f = Foo()
f.bar()
I get this unexpected output: (why did time.sleep not raise?)
before
after
However if I move the @mock.patch(...) down 1 line so it is decorating the method bar instead of the class Foo then it works as expected:
before
...
Exception: blah
Why does @mock.patch not work at the class level?
It turns out the class decorator only patches methods beginning with patch.TEST_PREFIX which defaults to test.
So renaming the method to test_bar or even testbar makes the patch start working.
Docs:
Patch can be used as a
TestCaseclass decorator. It works by decorating each test method in the class. This reduces the boilerplate code when your test methods share a common patchings set. patch() finds tests by looking for method names that start withpatch.TEST_PREFIX. By default this is'test', which matches the wayunittestfinds tests. You can specify an alternative prefix by settingpatch.TEST_PREFIX.
Evidently this behaviour applies to any class whether inheriting from unittest.TestCase or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With