考虑到复杂性,我无法提供太多的背景,但我希望能有一些洞察/发人深省的问题来解释为什么会发生这种情况
我正在测试一个将文件加载到数据库的进程,因此我正在使用unittest.mock.patch
修补数据库连接的凭据,以使用测试凭据而不是生产凭据。我们有一系列模拟,它们作为contextmanager
的简化版本应用于此:
from contextlib import ExitStack
def context_stack(contexts):
stack = ExitStack()
for context in contexts:
stack.enter_context(context)
return stack
def patch_mocks():
mocks = [
patch('db_config.ReadWrite', db_mocks.ReadWrite),
patch('db_config.ReadWrite', db_mocks.ReadWrite)
]
return context_stack(mocks)
它被这样使用(简化):
with patch_mocks():
LoadFiles(file_list)
LoadFiles
将迭代file_list
中的每个文件,并尝试将内容插入数据库。底层方法使用db_config.ReadWrite
连接到数据库,但当然它们是由db_mocks.ReadWrite
修补的。它的工作原理非常一致,除了看起来非常随机外,它将失败,因为它在尝试创建连接时尝试使用db_config.ReadWrite
例如,可能有100个文件,它会成功地修补其中大部分文件,但中途会随机停止使用修补程序,并导致测试失败。哪些条件/变量可能导致此修补程序无法应用?可以应用的补丁数量是否有限制?是否应该以另一种方式应用
我的第一条调查线将涉及来自the docs on .patch()的警告:
这是关于Where to patch的进一步解释
我会尝试找到一个破损的案例,并检查那里导入环境的状态,以确保从那里可以访问您在其他地方使用的相同导入
不要修补/模拟,而是使用repository pattern访问数据库
然后,您将拥有存储库接口的两个实现:
相关问题 更多 >
编程相关推荐