目前Python有两种纯测试的测试框架:Pytest和unittest
unittest应该是Python的老框架, 广为人知, 很多人都用来做自动化测试, 无论UI还是接口
Pytest是基于unittest开发的另一款更高级更好用的单元测试框架, 逼格高于unittest, 功能非常成熟且非常全,主要特点:
1. 简单灵活容易上手,文档丰富有很多实例可以参考
2. 支持参数化
3. 能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试,接口自动化测试(pytest+requests)
4. 执行测试过程中可以将某些测试跳过(skip),或者对某些预期失败的case标记成失败
5. 支持运行由nose,unittest编写的测试case
6. pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等
7. 测试用例的skip和xfail处理,可支持执行部分用例
8. 可以很好的和jenkins集成
9. report框架--allure也支持了pytest
安装
pip install pytest
1. 测试类主函数模式:运行testCase目录下test_01.py文件Test_one的测试用例test_01
#!/usr/bin/python3
# coding=utf-8
# Author: 文
import pytest
class Test_One():
def test_01(self):
print("____01")
assert 1 == 1
@pytest.mark.fail
def test_02(self):
print("____02")
assert 1 == 1
if __name__ == "__main__":
pytest.main(["testCase/test_01.py::Test_one:test_01"])
2. 命令行模式:运行testCase目录下test_01.py文件Test_one的测试用例test_01
pytest testCase/test_01.py::Test_one:test_01
Pytest Exit Code含义
Exit code 0:所有用例执行完毕,全部通过
Exit code 1:所有用例执行完毕,存在Failed的测试用例
Exit code 2:用户中断了测试的执行
Exit code 3:测试执行过程发生了内部错误
Exit code 4:pytest 命令行使用错误
Exit code 5:未采集到可用测试用例文件
Pytest信息
查看 pytest 版本:
pytest --version
显示可用的内置函数参数:
pytest --fixtures
命令行查看帮助信息:
pytest --help
命令行参数
1. 指定测试目录与测试模块
# 如:指定运行testCase目录下的test_01模块
......
if __name__ == "__main__":
pytest.main(["testCase/test_01.py"])
2. 通过指定nodeid执行测试用例(nodeid由模块文件名、分隔符(::)、类名、方法名、参数构成)
# 如下:运行test_one.py文件中对象Test_one下的测试用例test_02
......
if __name__ == "__main__":
pytest.main(["test_one.py::Test_One::test_02"])
3. 参数: -s 显示print的信息到控制台
......
if __name__ == "__main__":
pytest.main(["-s"])
4. 参数: --collect-only 收集将要执行的测试用例但不会执行测试用例
......
if __name__ == "__main__":
pytest.main(["--collcet-onty"])
5. 参数: -k 执行关键字表达式匹配筛选的测试用例
# 执行class名为Test_One的测试用例,但不执行Test_One的测试用例test_02
......
if __name__ == "__main__":
pytest.main(["-k=Test_One and not 02"])
6. 参数: -x 存在运行失败的用例立即停止测试
......
if __name__ == "__main__":
pytest.main(["-x"])
7. 参数: --maxfail=num 用例运行允许的最大失败次数num, 失败用例数超过num则立即停止测试
# 如:出现超过2个失败用例则终止测试
......
if __name__ == "__main__":
pytest.main(["--maxfail=2"])
8. 参数: --tb=选项(选项:'auto', 'long', 'short', 'no', 'line', 'native') 用例运行失败时展示错误的详细程度
......
if __name__ == "__main__":
pytest.main(["--tb=long"])
9. 参数: -l 或 --showlocals 用例运行失败时打印相关的局部变量
......
if __name__ == "__main__":
pytest.main(["-l"])
# pytest.main(["--showlocals"])
10. 参数: -v 打印用例执行的详细过程
......
if __name__ == "__main__":
pytest.main(["-v"])
11. 参数:-q 打印用例执行的简略过程
......
if __name__ == "__main__":
pytest.main(["-q"])
12. 参数: --lf 或 --last-failed 只执行上次执行失败的测试
......
if __name__ == "__main__":
pytest.main(["--lf"])
# pytest.main(["--last-failed"])
13. 参数: --ff / --failed-first 先执行上次失败的测试再执行上次正常的测试
......
if __name__ == "__main__":
pytest.main(["--ff"])
# pytest.main(["--failed-first"])
14. 参数: --durations=num -vv num=0则倒序显示所有的用例,为具体值则显示耗时最长的对应该数量的用例; -vv显示持续时间为0秒的用例
......
if __name__ == "__main__":
pytest.main(["--durations=0"]) # 按用例执行耗时倒序显示所有的用例信息
pytest.main(["--durations=2"]) # 显示运行耗时最长的2条测试用例,耗时小于0.01秒的则不显示
pytest.main(["--durations=2", "-vv"]) # 显示运行耗时最长的2条测试用例,耗时小于0.01秒的也显示
15. 参数: -m=name 只执行被@pytest.mark.name标记的测试用例
# 如:执行被装饰器@pytest.mark.fast或@pytest.mark.slow装饰的所有测试用例
......
if __name__ == "__main__":
pytest.main(["-m=fast or slow"])
16. 参数: ----pyargs=name 自动导入包name,并在该包所在的目录执行测试用例
# 如:导入包pkg.testing, 并在该包所在的目录执行测试用例
......
if __name__ == "__main__":
pytest.main(["--pyargs=pkg.testing"])
~ 不积跬步无以至千里