pytest7.4版本的一个变更,可能会影响你的项目
1|0pytest7.4版本的一个变更,可能会影响你的项目
本文撰写于 2023.7.10
2|0准备工作
-
项目结构如下
-
顶层conftest.py内容
-
pmCases下的conftest.py内容
-
test_logout.py内容
3|0Pytest7.4之前
用的Pytest7.3.1,而实际7.4.0之前也就只有一个7.3.2了
-
你是可以执行test_logout.py的
-
效果如下
-
所以按照以前的认识
- conftest可以存在多个
- 测试用例可以看到上级目录的conftest
- 但看不到下级目录的conftest(此处没有演示)
4|0Pytest7.4.0
4|1执行效果
注意把pytest更新到pytest7.4.0
-
同样执行test_logout.py
-
效果如下
-
很清楚的提示
-
子目录无法去引用上级目录的fixture
-
而同级目录不受影响
-
我们的实战课就会用到子目录下的测试文件调用上级目录的fixture,是没问题的,但现在会受影响。
-
这是为何呢?第一个想法就是版本变动了。但觉得不太可以理解,正常版本变动对这些逻辑不应该去大改,除非是大版本的改变。因为一旦出现这样的引用,你以前的项目会无法调用。
-
很多的时候你是在终端下执行
-
修改test_logout.py
-
终端下执行
-
这样执行
-
基于此,如果你是终端下执行的话,其实是没啥影响的。
-
只有你要在子目录下测试或者单独执行子测试用例时可能会有问题
- 带着这样的疑问去官方文档找原因。
4|2changlog Of pytest 7.4.0
发布时间 (2023-06-23)
Features
- #10901: Added
ExceptionInfo.from_exception()
, a simpler way to create anExceptionInfo
from an exception. This can replaceExceptionInfo.from_exc_info()
for most uses.
Improvements
-
#10872: Update test log report annotation to named tuple and fixed inconsistency in docs for
pytest_report_teststatus
hook. -
#10907: When an exception traceback to be displayed is completely filtered out (by mechanisms such as
__tracebackhide__
, internal frames, and similar), now only the exception string and the following message are shown:“All traceback entries are hidden. Pass
--full-trace
to see hidden and internal frames.”.Previously, the last frame of the traceback was shown, even though it was hidden.
-
#10940: Improved verbose output (
-vv
) ofskip
andxfail
reasons by performing text wrapping while leaving a clear margin for progress output.Added
TerminalReporter.wrap_write()
as a helper for that. -
#10991: Added handling of
%f
directive to print microseconds in log format options, such aslog-date-format
. -
#11005: Added the underlying exception to the cache provider’s path creation and write warning messages.
-
#11013: Added warning when
testpaths
is set, but paths are not found by glob. In this case, pytest will fall back to searching from the current directory. -
#11043: When
--confcutdir
is not specified, and there is no config file present, the conftest cutoff directory (--confcutdir
) is now set to the rootdir. Previously in such cases,conftest.py
files would be probed all the way to the root directory of the filesystem. If you are badly affected by this change, consider adding an empty config file to your desired cutoff directory, or explicitly set--confcutdir
. -
#11081: The
norecursedirs
check is now performed in apytest_ignore_collect
implementation, so plugins can affect it.If after updating to this version you see that your
norecursedirs
setting is not being respected, it means that a conftest or a plugin you use has a badpytest_ignore_collect
implementation. Most likely, your hook returnsFalse
for paths it does not want to ignore, which ends the processing and doesn’t allow other plugins, including pytest itself, to ignore the path. The fix is to returnNone
instead ofFalse
for paths your hook doesn’t want to ignore. -
#8711:
caplog.set_level()
andcaplog.at_level()
will temporarily enable the requestedlevel
iflevel
was disabled globally vialogging.disable(LEVEL)
.
Bug Fixes
- #10831: Terminal Reporting: Fixed bug when running in
--tb=line
mode wherepytest.fail(pytrace=False)
tests reportNone
. - #11068: Fixed the
--last-failed
whole-file skipping functionality (“skipped N files”) for non-python test files. - #11104: Fixed a regression in pytest 7.3.2 which caused to
testpaths
to be considered for loading initial conftests, even when it was not utilized (e.g. when explicit paths were given on the command line). Now thetestpaths
are only considered when they are in use. - #1904: Fixed traceback entries hidden with
__tracebackhide__ = True
still being shown for chained exceptions (parts after “… the above exception …” message). - #7781: Fix writing non-encodable text to log file when using
--debug
.
Improved Documentation
- #9146: Improved documentation for
caplog.set_level()
.
Trivial/Internal Changes
- #11031: Enhanced the CLI flag for
-c
to now include--config-file
to make it clear that this flag applies to the usage of a custom config file.
4|3抓重点
-
原文
-
译文
4|4解决方式
-
指定参数--confcutdir
-
示例1: test_logout.py执行
-
示例2: 终端执行
-
示例3: pytest.ini
- 注意不要写成
--confcutdir=.
(因为你是把pytest.ini放在根目录下的)
- 注意不要写成
4|5补充说明
-
截止到撰写本文的时候(2023-7-10)发现
-
pip install pytest会安装最新的Pytest7.4.0
-
而通过pycharm安装则是Pytest7.3.1
-
对于这个参数,命令行--help的解释是
__EOF__

本文链接:https://www.cnblogs.com/wuxianfeng023/p/17541307.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)