pytest7.4版本的一个变更,可能会影响你的项目
pytest7.4版本的一个变更,可能会影响你的项目
本文撰写于 2023.7.10
准备工作
项目结构如下
D:\Gitee\DemoRepo (17.97MB)
+-- testCases (1.03KB)
| +-- conftest.py (252b)
| +-- pmCases (574b)
| | +-- conftest.py (259b)
| | `-- test_logout.py (315b)
顶层conftest.py内容
import pytest @pytest.fixture(scope='session')
def fix_all():
print('fix_all')
pmCases下的conftest.py内容
import pytest @pytest.fixture(scope='session', autouse=True)
def fix_all2():
print('fix_all2')test_logout.py内容
import pytest def test_logout(fix_all):
print('test_logout') if __name__ == '__main__':
pytest.main(['-sv',__file__])
Pytest7.4之前
用的Pytest7.3.1,而实际7.4.0之前也就只有一个7.3.2了
你是可以执行test_logout.py的
效果如下
test_logout.py::test_logout fix_all2
fix_all
test_logout
PASSED
所以按照以前的认识
- conftest可以存在多个
- 测试用例可以看到上级目录的conftest
- 但看不到下级目录的conftest(此处没有演示)
Pytest4.0
执行效果
注意把pytest更新到pytest7.4.0
同样执行test_logout.py
效果如下
D:\Gitee\DemoRepo\venv\Scripts\python.exe D:/Gitee/DemoRepo/testCases/pmCases/test_logout.py
============================= test session starts =============================
platform win32 -- Python 3.9.6, pytest-7.4.0, pluggy-1.2.0 -- D:\Gitee\DemoRepo\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\Gitee\DemoRepo\testCases\pmCases
collecting ... collected 1 item test_logout.py::test_logout fix_all2
ERROR =================================== ERRORS ====================================
________________________ ERROR at setup of test_logout ________________________
file D:\Gitee\DemoRepo\testCases\pmCases\test_logout.py, line 10
def test_logout(fix_all):
E fixture 'fix_all' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, fix_all2, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them. D:\Gitee\DemoRepo\testCases\pmCases\test_logout.py:10
=========================== short test summary info ===========================
ERROR test_logout.py::test_logout
============================== 1 error in 0.01s =============================== 进程已结束,退出代码为 0很清楚的提示
E fixture 'fix_all' not found
子目录无法去引用上级目录的fixture
而同级目录不受影响
我们的实战课就会用到子目录下的测试文件调用上级目录的fixture,是没问题的,但现在会受影响。
这是为何呢?第一个想法就是版本变动了。但觉得不太可以理解,正常版本变动对这些逻辑不应该去大改,除非是大版本的改变。因为一旦出现这样的引用,你以前的项目会无法调用。
很多的时候你是在终端下执行
修改test_logout.py
def test_logout(fix_all):
print('test_logout')
终端下执行
D:\Gitee\DemoRepo\testCases>pytest
# 这是成功的
这样执行
D:\Gitee\DemoRepo\testCases\pmCases>pytest
# 报错跟上面一样 E fixture 'fix_all' not found基于此,如果你是终端下执行的话,其实是没啥影响的。
只有你要在子目录下测试或者单独执行子测试用例时可能会有问题
- 带着这样的疑问去官方文档找原因。
changlog Of pytest 7.4.0
发布时间 (2023-06-23)
Features
- #10901: Added
ExceptionInfo.from_exception(), a simpler way to create anExceptionInfofrom 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_teststatushook.#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-traceto see hidden and internal frames.”.Previously, the last frame of the traceback was shown, even though it was hidden.
#10940: Improved verbose output (
-vv) ofskipandxfailreasons 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
%fdirective 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
testpathsis set, but paths are not found by glob. In this case, pytest will fall back to searching from the current directory.#11043: When
--confcutdiris 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.pyfiles 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
norecursedirscheck is now performed in apytest_ignore_collectimplementation, so plugins can affect it.If after updating to this version you see that your
norecursedirssetting is not being respected, it means that a conftest or a plugin you use has a badpytest_ignore_collectimplementation. Most likely, your hook returnsFalsefor 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 returnNoneinstead ofFalsefor paths your hook doesn’t want to ignore.#8711:
caplog.set_level()andcaplog.at_level()will temporarily enable the requestedleveliflevelwas disabled globally vialogging.disable(LEVEL).
Bug Fixes
- #10831: Terminal Reporting: Fixed bug when running in
--tb=linemode wherepytest.fail(pytrace=False)tests reportNone. - #11068: Fixed the
--last-failedwhole-file skipping functionality (“skipped N files”) for non-python test files. - #11104: Fixed a regression in pytest 7.3.2 which caused to
testpathsto be considered for loading initial conftests, even when it was not utilized (e.g. when explicit paths were given on the command line). Now thetestpathsare only considered when they are in use. - #1904: Fixed traceback entries hidden with
__tracebackhide__ = Truestill 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
-cto now include--config-fileto make it clear that this flag applies to the usage of a custom config file.
抓重点
原文
When `--confcutdir` is not specified, and there is no config file present, the conftest cutoff directory (`--confcutdir`) is now set to the [rootdir](https://docs.pytest.org/en/7.4.x/reference/customize.html#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`.
译文
当未指定--confcutdir并且没有配置文件存在时,conftest截断目录(--confcutdir)现在被设置为rootdir。在以前的情况下,conftest.py文件会一直被探测到文件系统的根目录。如果你受到这个变化的严重影响,考虑在所需的截断目录中添加一个空的配置文件,或者明确地设置--confcutdir。
解决方式
指定参数--confcutdir
示例1: test_logout.py执行
import pytest def test_logout(fix_all):
print('test_logout') if __name__ == '__main__':
pytest.main(['-sv','--confcutdir=..',__file__]) # 意思是设定conftest.py的搜索根目录是当前目录上级
示例2: 终端执行
# 你在pmCases下执行
# 如果在项目根目录下,本来就是ok的
pytest --confcutdir=..
示例3: pytest.ini
[pytest]
# 改为实际的项目根目录即可
addopts = --confcutdir="D:\Gitee\DemoRepo"
- 注意不要写成
--confcutdir=.(因为你是把pytest.ini放在根目录下的)
- 注意不要写成
补充说明
截止到撰写本文的时候(2023-7-10)发现
pip install pytest会安装最新的Pytest7.4.0
而通过pycharm安装则是Pytest7.3.1
对于这个参数,命令行--help的解释是
--confcutdir=dir Only load conftest.py's relative to specified dir
pytest7.4版本的一个变更,可能会影响你的项目的更多相关文章
- Android从5.0到9.0版本的主要变更
https://www.jianshu.com/p/10bdbf883c46?utm_source=desktop&utm_medium=timeline Android5.0 1.虚拟机 在 ...
- dubbo的服务consumer与provider使用的api版本不一致,是否有影响
dubbo的服务consumer与provider使用的api版本不一致,是否有影响 最近新接手一个项目,看到定义的dubbo接口有些很奇葩的设定. 一.消费端 与 服务端 调用的接口中的数据包装类型 ...
- 解决iPhone上select时常失去焦点,随意跳到下一个输入框,影响用户操作
window.addEventListener('load', function() { FastClick.attach(document.body); }, false); //300s延迟,解决 ...
- 打造一个高逼格的android开源项目——小白全攻略 (转)
转自:打造一个高逼格的android开源项目 小引子 在平时的开发过程中,我们经常会查阅很多的资料,最常参考的是 github 的开源项目.通常在项目的主页面能看到项目的简介和基本使用,并且时不时能看 ...
- 发现一个名为“Douyu”的国人项目
刚刚在javaeye看到一个名为Douyu的国人项目,认为搞下去未来可能非常有意思,放到blog上做个标记. ——————下面是转载的作者原文——————— 原文地址例如以下:http://zhh20 ...
- 一个tomcat同时部署多个项目
一个tomcat同时部署多个项目 1. 注意事项: 1. 每一个service的端口号不能产生冲突 2. service的name属性的值可以重复 name="Catalina" ...
- 使用gulp搭建一个传统的多页面前端项目的开发环境
1.简介 使用gulp搭建一个传统的多页面前端项目的开发环境 支持pug scss es6编译支持 支持开发环境和打包生成sourceMap 支持文件变动自动刷新浏览器,css是热更新(css改动无需 ...
- 用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载
用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载,将一个完整的项目进行展示,主要有以下几个部分: 1.servlet部分 Export 2.工具类:TxtFileU ...
- mac中:不能完成此操作,因为找不到一个或多个需要的项目。(错误代码 -43)
今天使用mac删除某文件时,遇到此错误: 不能完成此操作,因为找不到一个或多个需要的项目.(错误代码 -43) 于是采用命令行删除可以正确删除:在要删除的文件夹坐在目录下执行 rm -rf tes ...
- IDEA如何导入一个web+maven以及如何运行项目
IDEA如何导入一个web+maven以及如何运行项目 然后就可以运行你的maven项目了....
随机推荐
- postgresSQL Extended Query执行过程和sharding-proxy的处理
pg Extended Query PostgreSQL: Documentation: 15: 55.2. Message Flow 多个阶段,可复用 Parse → DESCRIBE statem ...
- Java:如何加密或解密PDF文档?
在工作中,我们会将重要的文档进行加密,并且设置用户的访问权限,其他外部人员均无法打开,只有获取该权限的用户才有资格打开文档.此外,限制用户的使用权限,极大程度上阻止了那些有意要篡改.拷贝其中内容的人, ...
- 【数据结构与算法】无向图的结构与遍历 BFS&DFS
1 表示无向图的数据类型 1.1 邻接矩阵 可以使用一个V*V的二维布尔矩阵,当定点v和定点w相连的时候,定义第v行第w列的值为true,否则为false.邻接矩阵不适合定点较多的情况,含有百万的顶点 ...
- Spring Boot 中使用 Redis
Redis 环境 redis 安装.配置,启动:(此处以云服务器上进行说明) 下载地址:https://redis.io/download/ 下载后上传到云服务器上,如 /usr/local 中 gc ...
- opencv基础
Python 和 OpenCV 的结合是计算机视觉领域中应用最为广泛的一种方式,它们的结合使得开发者可以快速.高效地完成各种视觉任务.本文将介绍 Python 和 OpenCV 的基础使用,包括安装. ...
- Ffmpeg分布式视频转码问题总结
本文主要聊一聊云原生时代分布式转码系统实施过程中碰到的一些问题. 聊问题之前简单介绍一下我们的分布式转码方案. 云原生分布式转码 在计算资源招之即来的云计算时代,正在重构着软件架构的方方面面. 对软件 ...
- 基于SqlSugar的开发框架循序渐进介绍(28)-- 快速构建系统参数管理界面
在参照一些行业系统软件的时候,发现一个做的挺不错的系统功能-系统参数管理,相当于把任何一个基础的系统参数碎片化进行管理,每次可以读取一个值进行管理,这样有利于我们快速的处理业务需求,是一个挺好的功能. ...
- vue前端路由的两种模式,hash与history的区别
1.直观区别: hash模式url带#号,history模式不带#号. 2.深层区别: hash模式url里面永远带着#号,我们在开发当中默认使用这个模式. 如果用户考虑url的规范那么就需要使用hi ...
- C# 模拟界面点击/UI自动化测试
有一些UI自动化测试框架,能够实现自动化测试. 本文介绍Peer(微软的TAF技术),也可以实现自动化测试,或是对其他进程进行UI操作.下面是案例~ 在界面上添加俩个按钮: 并处理相应的点击事件: 1 ...
- 微信小程序搭建总结
小程序搭建总结 适合有基础的同志查阅,初学者也可以看看我的零基础小程序文章,需要总结文档留言给我或者公众号里面都有 1.项目的技术选型 第三方框架 1.腾讯 wepy 类似vue 2.美团 taro ...