安装
pip install -U pytest;
使用方式
代码示例:
import pytest
def test_a():
print("Test A")
assert 1 # 成功
def test_b():
print("Test B")
assert 0 # 失败
if __name__ == '__main__':
pytest.main(['-s', 'test_pytest.py'])
输出结果:
Pytest Exit Code含义清单
Exit code 0 所有用例执行完毕,全部通过
Exit code 1 所有用例执行完毕,存在Failed的测试用例
Exit code 2 用户中断了测试的执行
Exit code 3 测试执行过程发生了内部错误
Exit code 4 pytest 命令行使用错误
Exit code 5 未采集到可用测试用例文件
生成测试报告
1> 安装报告生成插件pytest-HTML
pip install pytest-html
2> 执行报告生成命令
pytest --html=./report.html
3> 目录下会生成assert目录和report.html结果报告文件