51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 647|回复: 2
打印 上一主题 下一主题

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

[复制链接]
  • TA的每日心情
    无聊
    2024-7-29 11:15
  • 签到天数: 32 天

    连续签到: 1 天

    [LV.5]测试团长

    跳转到指定楼层
    1#
    发表于 2023-7-21 11:20:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 韶光暗淡 于 2023-7-21 11:25 编辑

    2.0准备工作
    项目结构如下
    1. <font size="3">D:\Gitee\DemoRepo (17.97MB)
    2. +-- testCases (1.03KB)
    3. |   +-- conftest.py (252b)
    4. |   +-- pmCases (574b)
    5. |   |   +-- conftest.py (259b)
    6. |   |   `-- test_logout.py (315b)</font>
    复制代码
    顶层conftest.py内容
    1. <font size="3">import pytest

    2. @pytest.fixture(scope='session')
    3. def fix_all():
    4.     print('fix_all')</font>
    复制代码
    pmCases下的conftest.py内容
    1. <font size="3">import pytest

    2. @pytest.fixture(scope='session', autouse=True)
    3. def fix_all2():
    4.     print('fix_all2')
    5. </font>
    复制代码
    test_logout.py内容
    1. <font size="3">import pytest


    2. def test_logout(fix_all):
    3.     print('test_logout')

    4. if __name__ == '__main__':
    5.     pytest.main(['-sv',__file__])</font>
    复制代码
    3.0 Pytest7.4之前
    用的Pytest7.3.1,而实际7.4.0之前也就只有一个7.3.2了

    • 你是可以执行test_logout.py的
    • 效果如下



    1. <font size="3">test_logout.py::test_logout fix_all2
    2. fix_all
    3. test_logout
    4. PASSED</font>
    复制代码
    所以按照以前的认识
    • conftest可以存在多个
    • 测试用例可以看到上级目录的conftest
    • 但看不到下级目录的conftest(此处没有演示)

    4.0 Pytest7.4.04.1执行效果
    注意把pytest更新到pytest7.4.0
    • 同样执行test_logout.py
    • 效果如下
      1. <font size="3">D:\Gitee\DemoRepo\venv\Scripts\python.exe D:/Gitee/DemoRepo/testCases/pmCases/test_logout.py
      2. ============================= test session starts =============================
      3. platform win32 -- Python 3.9.6, pytest-7.4.0, pluggy-1.2.0 -- D:\Gitee\DemoRepo\venv\Scripts\python.exe
      4. cachedir: .pytest_cache
      5. rootdir: D:\Gitee\DemoRepo\testCases\pmCases
      6. collecting ... collected 1 item

      7. test_logout.py::test_logout fix_all2
      8. ERROR

      9. =================================== ERRORS ====================================
      10. ________________________ ERROR at setup of test_logout ________________________
      11. file D:\Gitee\DemoRepo\testCases\pmCases\test_logout.py, line 10
      12.   def test_logout(fix_all):
      13. E       fixture 'fix_all' not found
      14. >       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
      15. >       use 'pytest --fixtures [testpath]' for help on them.

      16. D:\Gitee\DemoRepo\testCases\pmCases\test_logout.py:10
      17. =========================== short test summary info ===========================
      18. ERROR test_logout.py::test_logout
      19. ============================== 1 error in 0.01s ===============================

      20. 进程已结束,退出代码为 0

      21. </font>
      复制代码
      很清楚的提示


    1. <font size="3">E       fixture 'fix_all' not found</font>
    复制代码

    • 子目录无法去引用上级目录的fixture
    • 而同级目录不受影响
    • 我们的实战课就会用到子目录下的测试文件调用上级目录的fixture,是没问题的,但现在会受影响。
    • 这是为何呢?第一个想法就是版本变动了。但觉得不太可以理解,正常版本变动对这些逻辑不应该去大改,除非是大版本的改变。因为一旦出现这样的引用,你以前的项目会无法调用。



    • 很多的时候你是在终端下执行
    • 修改test_logout.py


    1. <font size="3">def test_logout(fix_all):
    2.     print('test_logout')</font>
    复制代码

    [backcolor=rgba(255, 255, 255, 0.9)]终端下执行
    1. <font size="3">D:\Gitee\DemoRepo\testCases>pytest
    2. # 这是成功的</font>
    复制代码
    [backcolor=rgba(255, 255, 255, 0.9)]这样执行
    1. <font size="3">D:\Gitee\DemoRepo\testCases\pmCases>pytest
    2. # 报错跟上面一样  E       fixture 'fix_all' not found
    3. </font>
    复制代码
    • 基于此,如果你是终端下执行的话,其实是没啥影响的。
    • 只有你要在子目录下测试或者单独执行子测试用例时可能会有问题



    • 带着这样的疑问去官方文档找原因。



    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2024-7-29 11:15
  • 签到天数: 32 天

    连续签到: 1 天

    [LV.5]测试团长

    2#
     楼主| 发表于 2023-7-21 11:24:17 | 只看该作者
    4.2 changlog Of pytest 7.4.0https://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.


    4.3抓重点[backcolor=rgba(255, 255, 255, 0.9)]原文
    1. <font size="3">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`.
    2. </font>
    复制代码
    译文
    当未指定--confcutdir并且没有配置文件存在时,conftest截断目录(--confcutdir)现在被设置为rootdir。在以前的情况下,conftest.py文件会一直被探测到文件系统的根目录。如果你受到这个变化的严重影响,考虑在所需的截断目录中添加一个空的配置文件,或者明确地设置--confcutdir

    4.4解决方式
    • 指定参数--confcutdir
    • 示例1: test_logout.py执行


    1. <font size="3">import pytest


    2. def test_logout(fix_all):
    3.     print('test_logout')

    4. if __name__ == '__main__':
    5.     pytest.main(['-sv','--confcutdir=..',__file__]) # 意思是设定conftest.py的搜索根目录是当前目录上级</font>
    复制代码


    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2024-7-29 11:15
  • 签到天数: 32 天

    连续签到: 1 天

    [LV.5]测试团长

    3#
     楼主| 发表于 2023-7-21 11:24:25 | 只看该作者
    示例2: 终端执行
    1. <font size="3"># 你在pmCases下执行
    2. # 如果在项目根目录下,本来就是ok的
    3. pytest --confcutdir=..</font>
    复制代码

    示例3: pytest.ini
    1. <font size="3">[pytest]
    2. # 改为实际的项目根目录即可
    3. addopts = --confcutdir="D:\Gitee\DemoRepo"</font>
    复制代码


    • 注意不要写成--confcutdir=.(因为你是把pytest.ini放在根目录下的)


    4.5补充说明
    • 截止到撰写本文的时候(2023-7-10)发现
    • pip install pytest会安装最新的Pytest7.4.0
    • 而通过pycharm安装则是Pytest7.3.1
    • 对于这个参数,命令行--help的解释是


    1. <font size="3">  --confcutdir=dir Only load conftest.py's relative to specified dir</font>
    复制代码


    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-24 16:13 , Processed in 0.071443 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表