pytest.ini配置文件

发布于:2024-04-30 ⋅ 阅读:(26) ⋅ 点赞:(0)

概述

pytest.ini 是 Pytest 测试框架的配置文件,用于指定测试运行时的各种配置选项。通过编辑 pytest.ini 文件,可以定制测试运行的行为,例如设置测试目录、指定测试文件匹配模式、配置测试报告、定义测试标记等。

用途

pytest.ini 文件的主要用途是配置 Pytest 测试框架的行为,包括但不限于:

设置测试文件匹配模式: 可以通过 python_files 选项指定测试文件的匹配模式,使得 Pytest 只运行符合指定模式的测试文件。

指定测试文件目录: 使用 testpaths 选项可以指定 Pytest 在哪些目录下查找测试文件,这样可以灵活地组织测试代码。

添加命令行选项: 通过 addopts 选项可以添加额外的命令行选项,例如设置测试输出的详细程度、指定测试标记、设置并发测试等。

定义测试标记: 使用 markers 选项可以定义自定义的测试标记,以便对测试进行分类和管理,例如将一些测试标记为 smoke、regression 等。

严格处理 XFail 测试: 可以通过 xfail_strict 选项设置是否严格处理 XFail 测试,即是否要求 XFail 测试必须失败才算通过。

指定生成测试报告的格式: 使用 junit_family 选项可以指定生成 JUnitXML 格式的测试报告,以便与其他测试工具集成。

配置内容

执行pytest -h查看可用于pytest.ini的所有配置项,内容如下:

markers (linelist): 为测试函数注册新的标记
empty_parameter_set_mark (string): 空参数集的默认标记
norecursedirs (args): 递归时要避免搜索的目录模式
testpaths (args): 当命令行上没有给出文件或目录时,要搜索测试的目录
filterwarnings (linelist): 每行指定一个 warnings.filterwarnings 的模式。在 -W/--pythonwarnings 之后处理。
consider_namespace_packages (bool): 在导入时解析模块名称时考虑命名空间包
usefixtures (args): 要与此项目一起使用的默认装置列表
python_files (args): 用于 Python 测试模块发现的 Glob 样式文件模式
python_classes (args): 用于 Python 测试类发现的前缀或 Glob 名称
python_functions (args): 用于 Python 测试函数和方法发现的前缀或 Glob 名称
disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool): 禁用字符串转义非 ASCII 字符,可能导致意外的副作用(自行承担风险使用)
console_output_style (string): 控制台输出风格: "classic",或带有附加进度信息 ("progress" (百分比) | "count" | "progress-even-when-capture-no" (即使 capture=no 也强制显示进度)
verbosity_test_cases (string): 指定测试用例执行的详细程度,覆盖主要级别。更高的级别将提供有关每个执行的测试用例的更详细信息。
xfail_strict (bool): xfail 标记的 strict 参数的默认值(默认值:False)
tmp_path_retention_count (string): 根据 tmp_path_retention_policy 控制我们应该保留多少个 tmp_path 目录。
tmp_path_retention_policy (string): 根据测试结果控制由 tmp_path 装置创建的目录的保留情况。(all/failed/none)
enable_assertion_pass_hook (bool): 启用 pytest_assertion_pass 钩子。确保删除任何以前生成的 pyc 缓存文件。
verbosity_assertions (string): 指定断言的详细程度,覆盖主要级别。更高的级别将在断言失败时提供更详细的解释。
junit_suite_name (string): JUnit 报告的测试套件名称
junit_logging (string): 将捕获的日志消息写入 JUnit 报告: 选择 no|log|system-out|system-err|out-err|all 之一
junit_log_passing_tests (bool): 将捕获的日志信息包含在传递测试的 JUnit 报告中
junit_duration_report (string): 要报告的持续时间: total|call 中的一个
junit_family (string): 为模式发出 XML: legacy|xunit1|xunit2 中的一个
doctest_optionflags (args): doctests 的选项标志
doctest_encoding (string): 用于 doctest 文件的编码
cache_dir (string): 缓存目录路径
log_level (string): --log-level 的默认值
log_format (string): --log-format 的默认值
log_date_format (string): --log-date-format 的默认值
log_cli (bool): 在测试运行期间启用日志显示(也称为“实时日志记录”)
log_cli_level (string): --log-cli-level 的默认值
log_cli_format (string): --log-cli-format 的默认值
log_cli_date_format (string): --log-cli-date-format 的默认值
log_file (string): --log-file 的默认值
log_file_mode (string): --log-file-mode 的默认值
log_file_level (string): --log-file-level 的默认值
log_file_format (string): --log-file-format 的默认值
log_file_date_format (string): --log-file-date-format 的默认值
log_auto_indent (string): --log-auto-indent 的默认值
pythonpath (paths): 将路径添加到 sys.path
faulthandler_timeout (string): 如果测试花费超过 TIMEOUT 秒,则转储所有线程的回溯信息。

pytest.ini 文件中可以包含以下类型的配置:

命令行选项: 通过在 pytest.ini 文件中设置 addopts 选项,用户可以为 pytest 命令行工具提供默认参数,从而在每次运行 pytest 时自动应用这些参数。

插件配置: 使用 plugins 选项,用户可以指定要加载的 pytest 插件。这使用户能够启用额外的功能或集成其他测试工具,从而定制 pytest 的行为。

环境变量: 用户可以在 pytest.ini 文件中定义环境变量,这些变量可以在测试代码中通过标准的环境变量访问机制获取。这样可以在测试运行时动态地设置和使用环境变量。

测试发现: 通过配置选项,如 norecursedirs 和 testpaths,用户可以定制 pytest 如何发现测试。这些选项允许用户指定 pytest 在哪些目录下搜索测试文件,以及忽略哪些目录。

日志和其他设置: pytest.ini 还允许用户配置日志行为、修改测试报告格式等。通过设置相关的配置选项,用户可以调整 pytest 的日志输出和测试报告的格式,以满足特定的需求和偏好。

配置文件示例

# pytest.ini

# 命令行选项
[pytest]
addopts = -v -s --html=reports/test_report.html

# 插件配置
[pytest]
plugins =
    pytest-xdist
    pytest-cov

# 环境变量
[pytest]
env =
    ENVIRONMENT=development
    DATABASE_URL=sqlite:///:memory:

# 测试发现
[pytest]
testpaths = tests
norecursedirs = .* build dist CVS _darcs {arch} *.egg venv

# 日志和其他设置
[pytest]
log_format = %(asctime)s [%(levelname)s] %(message)s
log_date_format = %Y-%m-%d %H:%M:%S

addopts 配置

[pytest]
addopts = -v -s --html=reports/test_report.html

在这个示例中,addopts 设置了三个常用的 Pytest 命令行选项:

-v:增加输出的详细程度,显示每个测试用例的执行结果。
-s:允许将测试的输出打印到控制台,而不是被捕获并隐藏。
–html=reports/test_report.html:生成 HTML 格式的测试报告,并将其保存到指定的文件中(在这个例子中是 reports/test_report.html)。

收集用例规则配置

[pytest]

# 指定测试文件的匹配模式,以确定哪些文件包含测试用例
python_files =     test_*  *_test  test*
python_classes =   Test*   test*
python_functions = test_*  test*

# 指定要搜索测试文件的目录
testpaths = tests

# 指定不要递归搜索的目录
norecursedirs =
    .*     # 忽略隐藏目录
    build  # 忽略 build 目录
    dist   # 忽略 dist 目录
    CVS    # 忽略 CVS 目录
    _darcs # 忽略 _darcs 目录
    {arch} # 忽略特定版本控制目录
    *.egg  # 忽略 .egg 目录
    venv   # 忽略虚拟环境目录

在这个示例中:

python_files 配置了测试文件的匹配模式,表示只有文件名符合 test_*.py 的文件会被识别为测试文件,其中 * 表示通配符,可以匹配任意字符。这样可以避免误将非测试文件识别为测试文件。

testpaths 指定了测试文件所在的目录。在这个示例中,测试文件应该存放在名为 tests 的目录下。

norecursedirs 指定了不要递归搜索的目录列表。在这个示例中,配置了一系列目录,Pytest 在搜索测试文件时会忽略这些目录及其子目录。这样可以避免搜索和收集非测试相关的文件,提高测试文件的发现效率。

通过这些配置选项,你可以根据项目的组织结构和需求,定制 Pytest 如何发现和收集测试文件,使得测试运行更加高效和精确。


网站公告

今日签到

点亮在社区的每一天
去签到