-
项目结构如下
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