pytest测试框架
pytest测试框架
1.快速上手
安装:
pip install pytest
pip install pytest -U # 升级到最新版本
pytest三种启动方式:
-
命令
pytest -vv
-
代码
import pytestif __name__ == '__main__':pytest.main()
-
鼠标(不推荐)
pytest在简单的基础上,对断言进行高级封装(AST),对python数据结构断言非常友好,会精确的指出错误的地方
def test_str():a = '12345'b = '12344'
> assert a == b
E AssertionError: assert '12345' == '12344'
E
E - 12344
E ? ^
E + 12345
E ? ^
2.看懂结果
======================================================================================================== test session starts =========================================================================================================
platform win32 -- Python 3.11.9, pytest-8.4.1, pluggy-1.6.0 -- E:\work\pythonProject\PthonInAction_2025\CHAPTER4\pytestProj\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\work\pythonProject\PthonInAction_2025\CHAPTER4\pytestProj
collected 2 items tests/test_base.py::test_ok PASSED [ 50%]
tests/test_base.py::test_fail FAILED [100%] ============================================================================================================== FAILURES ==============================================================================================================
_____________________________________________________________________________________________________________ test_fail ______________________________________________________________________________________________________________ def test_fail():
> assert False
E assert Fals