python - 如何使用monkeypatching(PyTest)使用临时文件系统在python中模拟os.walk?

标签 python operating-system pytest os.walk

我有一个使用os.walk的函数,我想使用monkeypatching(PyTest)来测试它。我不知道如何使用 monkeypatch.setattr

最佳答案

要实现 os.walk 的正确模拟,您必须使用

monkeypatch.setattr(os, "walk", function_that_will_simulate_os_walk_iterator)

function_that_will_simulate_os_walk_iterator 应为每个模拟文件返回一个元组列表(root、dirs、files)

关于python - 如何使用monkeypatching(PyTest)使用临时文件系统在python中模拟os.walk?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46366376/

相关文章:

python - 在哪里将django文件放在服务器上?

python - 如何向已编译模块中的类添加方法?

c - Linux (CentOS) C 中的信号处理

c - 运行 fork 并打印 hello

python - 过滤 pytest 固定装置

python - pytest 或 py.test,以及不一致的文档

Python:如何使用 isinstance from parent 来确定它是否是特定的 child

python - coffeescript 字典设置为默认值

linux - 子进程创建期间的调度

python - 如何避免在 pytest_jira 插件的 jira.cfg 中硬编码用户名/密码