我在每个测试用例中都使用模拟装饰器。有什么方法可以将装饰器合并到 setUpClass
吗?
我的单元测试就像:
class TestCase():
@mock.patch('some.path.config')
def test_case_001(self, mock_cfg):
mock_cfg.return_value = 'value'
...
@mock.patch('some.path.config')
def test_case_002(self, mock_cfg):
mock_cfg.return_value = 'value'
...
@mock.patch('some.path.config')
def test_case_003(self, mock_cfg):
mock_cfg.return_value = 'value'
...
我可以将模拟合并到一个模拟中吗:
class TestCase():
@mock.patch('some.path.config')
@classmethod
def setUpClass(cls, mock_cfg):
mock_cfg.return_value = 'value'
pass
def test_case_001(self):
pass
def test_case_002(self):
pass
def test_case_003(self):
pass
最佳答案
用这样的方法,应该有用。
class TestDemo(test.TestCase):
def setUp(self):
super(TestDemo, self).setUp()
self.mocks = [(mock.patch('a.methodB',
mock.MagicMock(return_value=None)))]
for single_mock in self.mocks:
single_mock.start()
self.addCleanup(single_mock.stop)
关于python - 我可以在 setUpClass 中模拟多个测试用例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54212072/