
pytest 3.4.2 版本解析Bug 修复、-c配置识别与approx对Decimal的支持【免费下载链接】pytestThe pytest framework makes it easy to write small tests, yet scales to support complex functional testing项目地址: https://gitcode.com/GitHub_Trending/py/pytestpytest 3.4.2 是 pytest 框架在 2018 年 3 月发布的一个纯 Bug 修复bug-fix版本定位为pip install --upgrade pytest即可无缝替换的 drop-in replacement。本文以官方发布公告与仓库中的 changelog 记录为主体结合当前仓库src/_pytest下的源码实现逐条拆解本次修复的四个关键缺陷-s捕获关闭时的进度输出、bindir 判断方式、approx与Decimal的TypeError以及requestfixture 的引用环并给出可直接复现、验证的实操命令。发布概述一次无痛升级的补丁版本根据 release-3.4.2.rst 公告pytest 3.4.2 在发布当日即已同步到 PyPI升级命令为pip install --upgrade pytest该版本不引入任何新特性也不包含破坏性变更属于标准的补丁节奏相邻的 3.4.0 是特性版本、3.4.1 是另一轮修复可参见 changelog.rst 中的版本脉络。公告同时感谢了 Allan Feldman、Bruno Oliveira、Jason R. Coombs、Kyle Altendorf 等十一位贡献者并提示完整变更日志见官方 changelog——在本仓库中这份日志的 3.4.2 章节位于 doc/en/changelog.rst。从仓库中 changelog.rst 的完整记录看3.4.2 的变更共分三类Bug Fixes4 项issue #3203、#3241、#3247、#3249、#3260其中 #3260 归在修复类下Improved Documentation1 项issue #3209将 logging 插件补充进插件列表Trivial/Internal Changes1 项issue #3259修正 fixture.rst 中的拼写。修复一-s关闭捕获时不再输出进度信息问题背景issue #3203 报告的缺陷是当用户通过-s即--captureno关闭输出捕获时pytest 仍会在终端上输出进度信息。由于此时测试程序自身的 stdout/stderr 不再被拦截进度字符与用户程序输出混在一起导致终端内容严重错乱几乎无法阅读。源码中的落地实现在当前的 src/_pytest/terminal.py 中TerminalReporter._determine_show_progress_info()明确注释了这条修复的历史来源def _determine_show_progress_info(self) - Literal[progress, count, times, False]: Return whether we should display progress information based on the current config. # do not show progress if we are not capturing output (#3038) unless explicitly # overridden by progress-even-when-capture-no if not self._capture_enabled and self.config.getini(console_output_style) \ ! progress-even-when-capture-no: return False ...这段代码与 3.4.0 引入的控制台classic 回退逻辑issue #3038一脉相承默认的console_output_style为progress见 terminal.py当捕获被关闭时自动隐藏进度条如果用户确实希望在-s下也看到进度则可以显式设置console_output_style progress-even-when-capture-no。实操验证# 观察 -s 下不再出现进度百分比/计数 pytest -s testing/python/collect.py # 若仍需要进度信息可在 pytest.ini 中显式打开 # pytest.ini: # [pytest] # console_output_style progress-even-when-capture-no可选的console_output_style取值由_ConsoleOutputStyle类型定义terminal.pyclassic、progress默认、count、times与progress-even-when-capture-no并可通过--console-output-style命令行选项在运行时覆盖terminal.py 附近。修复二bindir 判断从exists改为isdir问题背景issue #3241 指出此前代码使用路径是否存在exists来判断 bindir二进制目录但存在并不等价于是一个目录——例如路径指向一个普通文件时同样会通过exists检查造成语义误判。修复含义修复后改用是否为目录isdir来判断。这属于路径语义上的收紧只有真正是目录的路径才会被当作 bindir 处理。在本仓库中路径判断相关的辅助函数集中在 src/_pytest/_py/path.py包括check()、isdir()、isfile()等路径检查 API后续版本中 pytest 进一步将路径抽象迁移到pathlib体系参见 src/_pytest/pathlib.py但isdir这一判断语义被保留下来。这是一个低风险修复理论上不改变正常项目的行为但避免了文件路径被误判为目录带来的边界错误。修复三approx对Decimal的比较不再抛出TypeError问题背景issue #3247 报告的缺陷是pytest.approx在期望值或实际值为decimal.Decimal时会抛出TypeError导致涉及高精度十进制数值如金融计算、金额断言的测试无法正常使用近似比较。源码中的落地实现在当前的 src/_pytest/approx.py 中Decimal已被完整纳入近似比较体系文件头部直接from decimal import Decimalapprox.py_approx_scalar()会针对Decimal分派到专门的ApproxDecimal类approx.pyApproxDecimal重新定义了适合十进制语义的默认容差DEFAULT_ABSOLUTE_TOLERANCE Decimal(1e-12)、DEFAULT_RELATIVE_TOLERANCE Decimal(1e-6)approx.py并在传入 float 型容差时通过Decimal.from_float()精确转换approx.py顶层approx()入口也会对Decimal期望值直接返回ApproxDecimal实例approx.py。实操示例# 文件test_approx_decimal.py from decimal import Decimal import pytest def test_approx_decimal(): # 3.4.2 之前会抛 TypeError修复后可正常近似比较 assert Decimal(3.141592653589793) pytest.approx(Decimal(3.141592653589)) # 默认相对容差为 Decimal(1e-6) assert Decimal(100) pytest.approx(Decimal(100.00005)) # float 期望值 Decimal 实际值同样受支持 assert Decimal(2.71828) pytest.approx(2.71828, rel1e-4)运行pytest test_approx_decimal.py -v如果不需要Decimal专用的1e-6/1e-12容差也可以显式传入rel/absApproxScalar会校验二者必须是int、float或Decimal参见 approx.py。修复四requestfixture 不再产生引用环问题背景issue #3249 修复了使用requestfixture 时可能生成的引用循环reference cycle问题。引用环会阻碍垃圾回收器及时回收对象在大量测试、长驻进程或结合内存分析工具时可能导致内存占用偏高。修复含义request是 pytest 内置的核心 fixture承载了当前测试项的上下文节点、配置、参数化信息、fixturename等。该修复的目标是打破request与其底层节点对象之间可能互相持有的强引用链让对象生命周期结束后可以被正常回收。在现行代码中request相关的节点访问接口集中在 src/_pytest/fixtures.pyFixtureRequest的实现_pytest.nodes则定义了节点间的层级关系src/_pytest/nodes.py。对于使用者来说这是一次透明修复——不需要修改任何测试代码但内存密集型测试套件可以受益于更及时的对象回收。附带的文档与内部改进除上述四项修复外3.4.2 还包含两处小改动changelog.rst文档issue #3209在官方插件列表中加入 logging 插件条目方便用户发现pytest内置的日志捕获功能对应源码为 src/_pytest/logging.py配套文档见 doc/en/how-to/logging.rst内部issue #3259修正 fixture 文档中的一处拼写错误。这类改动不影响运行时行为但对文档可发现性与可读性有直接价值。升级与验证建议3.4.2 是 drop-in replacement升级与验证流程非常简单# 1. 升级 pip install --upgrade pytest # 2. 确认版本 pytest --version # 应输出 3.4.2 或更高版本 # 3. 快速回归 pytest -q如果此前曾在-s模式下依赖进度输出或大量使用Decimal近似断言建议分别补做对应场景的冒烟测试确认行为符合预期。完整的逐条变更记录可在仓库的 doc/en/changelog.rst 中查阅。【免费下载链接】pytestThe pytest framework makes it easy to write small tests, yet scales to support complex functional testing项目地址: https://gitcode.com/GitHub_Trending/py/pytest创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考