1
我有一个测试案例,在setUp中,我创建了一个对象,我想在模块uuid
中模拟函数uuid4
。现在如何在测试用例中模拟uuid代?
TEST_UUIDS = ['uuid_{}'.format(i) for i in range(10000)]
UUID_POOL = iter(TEST_UUIDS)
def generate_uuid() -> str:
return next(UUID_POOL)
class MyTestCase(TestCase):
def setUp(self, **kwargs):
self._create_my_object
@patch.object(uuid, 'uuid4', generate_uuid)
def _create_my_object(self):
# Create an object using a uuid
我的问题是,当我运行写两个测试案例,创建对象第二次,它得到其他的UUID是第一次。因此,结果取决于测试类中测试用例的数量和它们运行的顺序,这是我不想要的。
- 这是嘲笑uuid发电机的最好方法吗?
- 如何在每次setUp(或tearDown)调用时重置或重新创建迭代器?
更多细节可以在这里找到:https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.side_effect –