首页 > 其他分享 >pytest7.4版本的一个变更,可能会影响你的项目

pytest7.4版本的一个变更,可能会影响你的项目

时间:2023-07-10 15:45:45浏览次数:39  
标签:-- py conftest pytest logout 版本 test 变更 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

https://docs.pytest.org/en/7.4.x/changelog.html#

发布时间 (2023-06-23)

Features

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) of skip and xfail 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 as log-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 a pytest_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 bad pytest_ignore_collect implementation. Most likely, your hook returns False 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 return None instead of False for paths your hook doesn’t want to ignore.

  • #8711: caplog.set_level() and caplog.at_level() will temporarily enable the requested level if level was disabled globally via logging.disable(LEVEL).

Bug Fixes

  • #10831: Terminal Reporting: Fixed bug when running in --tb=line mode where pytest.fail(pytrace=False) tests report None.
  • #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 the testpaths 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

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.

抓重点

  • 原文

    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
    

标签:--,py,conftest,pytest,logout,版本,test,变更,pytest7.4
From: https://www.cnblogs.com/wuxianfeng023/p/17541307.html

相关文章

  • ionic cordova 打包Rlease版本包出现异常Execution failed for task ':app:mergeRelea
    异常: 解决方法:找到android=》app下的build.gradle文件,如下增加如下配置 运行ioniccordovabuildandroid--release打包语句正常执行 ......
  • 2023最新版本WebStrom安装教程【2023.1.3】
    前言本文方法可以安装使用截止当前2023.1.3最新版本WebStrom,过程非常简单,按照下面的步骤来一分钟即可搞定。1.下载安装已经安装过的可以跳过该步骤!下载到官网地址下载正版安装包JetBrainsWebStrom官网下载地址安装开始安装选择安装路径桌面快捷方式勾选创建妆......
  • 开源ERP软件odoo15社区版本安装教程
    开源ERP软件odoo15社区版本安装教程odoo版本:odoo15社区版操作系统:UbuntuServer20.04LTS64bit1.内容介绍odoo是一款基于Python和PostgreSQL开发的ERP/CRM网站程序。本文主要介绍如何通过安装包的方式在Ubuntu系统上进行安装。服务器使用云端服务器。内容包括服......
  • Windows下安装python2和python3双版本及问题解决
    现在大家常用的桌面操作系统有:Windows、MacOS、ubuntu,其中MacOS和ubuntu上都会自带python。这里我们只介绍下Windows(我用的Win10)环境下的python2.x和python3.x的安装,以及python2.x与python3.x共存时的配置问题。本节内容python下载安装Python2.x安装Python3.x当前存......
  • 切换node版本,npm版本对应
    1、控制nodejs版本可以使用模块n来管理,首先安装模块nnpminstall-gn2、将node升级到稳定版本sudonstable3、安装最新版本sudonlatest4、安装指定版本(最好用)sudonv14.19.0//版本v自定义5、检查目前安装了哪些版本的node,会出现已安装的node版本,选个就可以直接切换了n......
  • Windows下,多个版本jdk的切换
    1.安装jdk正常到oracle官网安装即可2.版本管理工具——jenvwindows版本使用jenvforwindowshttps://github.com/FelixSelter/JEnv-for-Windows(其他系统安装使用jenv即可)(arhlinux可以直接使用archlinux-java命令)到release界面,下载解压JENV.zip将解压路径添加到环境变量......
  • node 版本管理工具
    1、sudonpminstalln-g  支持mac 2、nvm    下载地址:https://github.com/coreybutler/nvm-windows/releases 下载后安装即可 ......
  • 工作总结之git版本穿梭
    目录工作总结之git版本穿梭前言简单穿梭(单纯的回到过去和未来)在过去的时候push了新的代码是否还能回到未来(原来的最新版本)工作总结之git版本穿梭前言前段时间探索了下git的版本回退,然后前两天突然想到,如果回退后悔了怎么办,本次就是来探究这个问题的简单穿梭(单纯的回到过去和......
  • 升级Elasticsearch到8.7.1版本,我给自己挖了很多坑......
    转载说明:如果您喜欢这篇文章并打算转载它,请私信作者取得授权。感谢您喜爱本文,请文明转载,谢谢。最近因为某些原因,需要将ES升级到8.7.1版本。之前用的ES版本比较老了,这次部署新版本ES,发现变化蛮大的,一小心又踩了一些坑,还给自己挖了一些坑......1.java版本不符合需求,es启动失败报错内......
  • error NU1803: 错误形式的警告: 正在通过 “HTTP” 源“http://apricot.com/repositor
    一、私有仓库错误(vs2022)错误信息errorNU1803:错误形式的警告:正在通过“HTTP”源“http://apricot.com/repository/nuget-group/”运行“restore”操作。将来的版本中将删除非HTTPS访问权限。请考虑迁移到“HTTPS”源。错误截图二、解决&处理打开Nuget配置%APP......