[英]pytest tmpdir_factory test glob search
I'm trying to write a test that validates that the correct files are found when I glob over a directory but I dont understand why the glob is coming back empty using the tmpdir_factory, as executing the code on a real dir works as expected. 我正在尝试编写一个测试,以验证在遍历目录时是否找到了正确的文件,但是我不明白为什么使用tmpdir_factory将遍历恢复为空,因为在真实目录上执行代码可以按预期工作。 All the module imports are correct and it is calling the function.
所有模块导入均正确,并且正在调用该函数。
Code to be tested: 要测试的代码:
def get_files(sdir, extension):
return glob.glob1(sdir, "*" + extension + "*")
pytest code: pytest代码:
flist = [
"12707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017011",
"22707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017012",
"32707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017013",
"42707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017014",
"52707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017015",
"12707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017011.pdpr",
"22707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017012.pdpr",
"32707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017013.pdpr",
"42707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017014.pdpr",
"52707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017015.pdpr"
]
def test_get_files_afp(tmpdir_factory):
rlist = [
"12707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017011",
"22707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017012",
"32707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017013",
"42707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017014",
"52707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017015"
]
test_dir = tmpdir_factory.mktemp('testdata')
for f in flist:
test_dir.join(f)
lst = get_files(sdir=str(test_dir), extension=".afp")
assert lst == rlist
fails with: 失败与:
assert lst == rlist
E AssertionError: assert [] == ['12707054321.HOM0LRP2.COUNT....JOB46147.pg96.afp.2017015']
E Right contains more items, first extra item:
'12707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017011'
E Full diff:
E - []
E + ['12707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017011',
E + '22707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017012',
E + '32707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017013',
E + '42707054321.HOM0LRP2.COUNTS.JOB46147.pg96.afp.2017014',...
In tmpdir/tmpdir_factory the file is not actually created until you write to it. 在tmpdir / tmpdir_factory中,直到您写入文件才真正创建该文件。
changing: 改变:
for f in flist:
test_dir.join(f)
to 至
for f in flist:
a = test_dir.join(f)
a.write('')
Causes the file to be created and works as expected 导致文件被创建并按预期工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.