我正在尝试构建一个例程,为当前目录中的每个 PDF 文档调用 Pytest 类......让我解释一下
假设我有这个测试文件
import pytest
class TestHeader:
#asserts...
class TestBody:
#asserts...
此脚本需要测试我的 cwd 中的每个 pdf 文档这是我最好的尝试:
import glob
import pytest
class TestHeader:
#asserts...
class TestBody:
#asserts...
filelist = glob.glob('*.pdf')
for file in filelist:
#magically call pytest for each file
我将如何处理这个?编辑:补充我的问题。
我有一个巨大的函数来提取每个文档的数据,我们称之为
extract_pdf
此函数返回一个元组(标题,正文)。当前的尝试如下所示:
import glob
import pytest
class TestHeader:
#asserts...
class TestBody:
#asserts...
filelist = glob.glob('*.pdf')
for file in filelist:
header, body = extract_pdf(file)
pytest.main(<pass header and body as args for pytest>)
我需要在测试之前解析每个文档。可以这样做吗?
最佳答案
通过动态参数化测试用例来做到这一点的最佳方法..
这可以使用 pytest_generate_tests
来实现钩..
def pytest_generate_tests(metafunc):
filelist = glob.glob('*.pdf')
metafunc.parametrize("fileName", filelist )
注意:fileName
应该是您的测试函数的参数之一。这将导致为目录中的每个文件执行测试用例,测试用例将类似于
TestFunc[File1]
TestFunc[File2]
TestFunc[File3]
.
.
等等..
关于python - 为目录中的每个文件运行 pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63210834/