1.安装
pip install pytest
查看版本信息
pytest --version
This is pytest version 5.4.3, imported from /usr/local/lib/python3.8/site-packages/pytest/__init__.py
2.编写pytest的"hello world"
编写test_sample.py,测试inc函数
# content of test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
在当前目录运行pytest命令,pytest自动查找测试例并运行
# pytest
==================================================================== test session starts =====================================================================
platform linux -- Python 3.8.2, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /home/sources/pytests
collected 1 item
test_example.py F [100%]
========================================================================== FAILURES =========================================================&