gpt4 book ai didi

python - pytest tmpdir_factory 测试 glob 搜索

转载 作者:行者123 更新时间:2023-11-28 20:49:54 24 4
gpt4 key购买 nike

我正在尝试编写一个测试来验证当我在一个目录上 glob 时是否找到了正确的文件,但我不明白为什么使用 tmpdir_factory glob 返回为空,因为在真实目录上执行代码就像预期的。所有模块导入都是正确的,它正在调用该函数。

待测代码:

def get_files(sdir, extension):
return glob.glob1(sdir, "*" + extension + "*")

测试代码:

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

失败:

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',...

最佳答案

在 tmpdir/tmpdir_factory 中,文件在您写入之前不会真正创建。

变化:

for f in flist:
test_dir.join(f)

for f in flist:
a = test_dir.join(f)
a.write('')

创建文件并按预期工作

关于python - pytest tmpdir_factory 测试 glob 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50944928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com