TA的每日心情 | 无聊 2024-7-29 11:15 |
---|
签到天数: 32 天 连续签到: 1 天 [LV.5]测试团长
|
本帖最后由 韶光暗淡 于 2023-7-21 11:25 编辑
2.0准备工作
项目结构如下
- <font size="3">D:\Gitee\DemoRepo (17.97MB)
- +-- testCases (1.03KB)
- | +-- conftest.py (252b)
- | +-- pmCases (574b)
- | | +-- conftest.py (259b)
- | | `-- test_logout.py (315b)</font>
复制代码 顶层conftest.py内容
- <font size="3">import pytest
- @pytest.fixture(scope='session')
- def fix_all():
- print('fix_all')</font>
复制代码 pmCases下的conftest.py内容
- <font size="3">import pytest
- @pytest.fixture(scope='session', autouse=True)
- def fix_all2():
- print('fix_all2')
- </font>
复制代码 test_logout.py内容
- <font size="3">import pytest
- def test_logout(fix_all):
- print('test_logout')
- if __name__ == '__main__':
- pytest.main(['-sv',__file__])</font>
复制代码 3.0 Pytest7.4之前
用的Pytest7.3.1,而实际7.4.0之前也就只有一个7.3.2了
你是可以执行test_logout.py的 效果如下
- <font size="3">test_logout.py::test_logout fix_all2
- fix_all
- test_logout
- PASSED</font>
复制代码所以按照以前的认识 - conftest可以存在多个
- 测试用例可以看到上级目录的conftest
- 但看不到下级目录的conftest(此处没有演示)
4.0 Pytest7.4.04.1执行效果
注意把pytest更新到pytest7.4.0
同样执行test_logout.py 效果如下 - <font size="3">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
- </font>
复制代码 很清楚的提示
- <font size="3">E fixture 'fix_all' not found</font>
复制代码
很多的时候你是在终端下执行 修改test_logout.py
- <font size="3">def test_logout(fix_all):
- print('test_logout')</font>
复制代码
[backcolor=rgba(255, 255, 255, 0.9)]终端下执行- <font size="3">D:\Gitee\DemoRepo\testCases>pytest
- # 这是成功的</font>
复制代码 [backcolor=rgba(255, 255, 255, 0.9)]这样执行
- <font size="3">D:\Gitee\DemoRepo\testCases\pmCases>pytest
- # 报错跟上面一样 E fixture 'fix_all' not found
- </font>
复制代码
|
|