【发布时间】:2012-05-30 16:40:15
【问题描述】:
我有一个 python 方法执行以下操作:
- 使用 os.listdir(/test) 列出目录下的文件
- 正则表达式匹配目录下的部分文件,将文件放在一个列表中
- 从列表中的文件中读取内容,进行一些聚合。
显然,在我的案例中,我要测试的唯一有趣的部分是 2、3,所以 1 绝对是我想要模拟的东西。我开始在 setUp() 和 tearDown() 的 /test 文件夹下创建/删除补丁文件。但是同事告诉我,在 unitest 中进行 I/O 并不是一个好主意。
那么在我的 unitest 中模拟构建 os.listdir() 的最佳方法是什么?或者有什么替代方案?
我可以做些什么来实现以下目标:
setUp() {
#mock a few files eg.test1.txt, test2.txt, test3.txt under directory /test
#without physically creating them using I/O
}
tearDown() {
#whatever cleanup required
}
【问题讨论】:
标签: python unit-testing python-mock