python - 为目录中的每个文件运行 pytest

标签 python flask pytest glob

我正在尝试构建一个例程,为当前目录中的每个 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/

相关文章:

python - Openslide python导入错误

python - 本程序从哪里获取json_myobj

python - Flask - 基本身份验证的装饰器

ubuntu - Pytest + GDAL : cannot allocate memory in static TLS block

python - 从另一个字符串中删除一个字符串

python - docker 化时 pip 要求不匹配

python - 使 sql 转储可下载

javascript - 从 Flask 后端 react axios GET 空数据

python - 从 emacs 运行 py.test

python - 在父项目上运行 pytest 时,如何忽略 git 子模块根目录下的 conftest.py 文件(例如 submodule/conftest.py)?