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

在更新到pytest 7.4.0后,子目录的测试文件无法调用上级目录的fixture,导致错误`fixture 'fix_all' not found`。这个问题源于`--confcutdir`的更改,现在默认设定为rootdir。解决办法包括:1) 在执行时指定`--confcutdir`参数;2) 在终端执行时添加完整路径;3) 在pytest.ini中设置`confcutdir`。
摘要由CSDN通过智能技术生成
  • 项目结构如下

     

    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__])

3|0Pytest7.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(此处没有演示)

4|0Pytest7.4.0

4|1执行效果

注意把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, caps

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值