当一个方法应该在测试用例中被模拟时,可以在 python框架中应用@mock.patch
装饰器(参见1):unittest
class MyTest(TestCase):
@patch('method2')
@patch('method1')
def test_stuff(self, mock_method1, mock_method_2):
...
根据文档2,也可以将 应用@mock.patch
为类装饰器:
@patch('method2')
@patch('method1')
class MyTest(TestCase):
def test_stuff(self, mock_method_1, mock_method_2):
...
因此,将这两种方法结合起来也应该是可能和合理的:
@patch('method1')
class MyTest(TestCase):
@patch('method2')
def test_stuff(self, mock_method_A, mock_method_B):
...
现在我想知道模拟以什么顺序传递给test_stuff
. mock_method_A
模拟method1
or也是如此method2
?