我在一个类中有几个测试方法,它们对对象使用一种类型的修补,所以我用类装饰器进行了修补。对于另一种方法,我想以不同的方式修补同一个对象。我尝试了以下方法,但是尽管方法本身被不同的补丁装饰,但作为类装饰器制作的补丁仍然有效。我希望方法补丁覆盖类补丁。为什么不是这样?
在这种特殊情况下,我可以删除类补丁并修补单个方法,但这将是重复的。如何实现这种覆盖(方法覆盖类补丁)机制?
from unittest TestCase
from unittest import mock
@mock.patch('my_module.cls.method', mock.Mock(side_effect=RuntimeError('testing'))
class SwitchViewTest(TestCase):
def test_use_class_patching(self):
# several other methods like this
# test code ..
@mock.patch('my_module.cls.method', mock.Mock(side_effect=RuntimeError('custom'))
def test_override_class_patching(self):
# test code ...