1.安装
pip install -U pytest
2.查看版本
1. pytest --version 2. # 或 3. pip show pytest
3.创建第一个测试
1.新建test_sample.py
1. def func(x): 2. return x + 1 5. def test_answer(): 6. assert func(3) == 5
2.命令行执行,切到文件所在目录,执行pytest,结果如下:
3.pytest运行规则:当前目录及其子目录中运行所有格式为test _ *.py或*_test.py
4.pytest用例规则
- 测试文件以test_开头(以_test结尾也可以)
- 测试类以Test开头,并且不能带有 __init__ 方法
- 测试函数以test_开头
- 断言使用assert