You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pytest conftest.py实现文档查询及版本加载逻辑差异咨询

pytest版本间conftest.py加载逻辑差异问题

问题背景

  • pytest 7.2.2运行测试时,会向上遍历目录树,合并加载所有上级目录中的conftest.py文件
  • 升级到pytest 8.3.3后,仅加载当前运行测试的目录下的conftest.py,上级目录的同名文件不再被加载
  • 未找到官方文档中明确说明这一实现机制变更的内容

版本运行输出对比

pytest 7.2.2 输出

> $ pytest test_simple_module.py -s                                                                                         [±main ●]
CONFTEST ../../ LOADED
CONFTEST .. LOADED
CONFTEST . LOADED
======================================================== test session starts =========================================================
platform darwin -- Python 3.11.10, pytest-7.2.2, pluggy-1.0.0
rootdir: /Users/steve/python/tests/TestUtils/PytestWrapper/simple_module
plugins: anyio-3.7.1
collecting ...
Collected test items:

pytest 8.3.3 输出

(test_env) > $ pytest -s test_simple_module.py                                                                                  [±main ●]
CONFTEST . LOADED
========================================================== test session starts ===========================================================
platform darwin -- Python 3.12.7, pytest-8.3.3, pluggy-1.5.0
rootdir: /Users/steve/python/tests/TestUtils/PytestWrapper/simple_module
collected 7 items

核心差异

pytest 7.2.2会加载当前目录及所有上级目录的conftest.py,而8.3.3仅加载当前运行测试的目录下的conftest.py。

排查建议

  • 查阅pytest 8.x系列的版本变更日志,重点关注与conftest.py加载逻辑、目录遍历规则相关的更新
  • 尝试在上级目录执行测试命令,观察8.3.3是否会加载下级目录的conftest.py,验证加载逻辑的范围变化
  • 检查测试命令的执行参数是否一致,确认无额外参数影响conftest.py的加载范围

内容的提问来源于stack exchange,提问作者StevieD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 14:00:59