Pytest入门

134 阅读1分钟

安装

pip insatll pytest

导入

import pytest

要求

模块名以test开头或者以_test结尾 测试类必须以Test开头,而且不能有init方法 测试方法必须以test开头

运行

1.主函数模式

  • 执行所有用例
    if __name__=='__main__': pytest.main(['-vs'])

image.png

  • 执行部分用例
    pytest.main(['-vs','test_01.py'])

image.png

-执行指定文件夹用例
pytest.main(['-vs','/dir'])

-通过nodeid指定执行

image.png

2.命令行模式
在命令行里执行pytest命令,参数和上面一致
例如 pytest -sv test_01.py

3.通过pytest.init配置文件运行

image.png

[pytest]
addopts = -vs
testpaths = ./dir
python_files = a*.py
python_classes = Test*
python_functions = test

参数

  • -s 输出调试信息
  • -v 输出用例详情信息
  • -n 多线程来运行测试用例
  • --reruns=2 失败的用例重跑2次(命令行为 --reruns 2)
  • -x 只要错一个用例,所有停止
  • -maxfail=2 出现两个失败用例就停止
  • -k 指定含特定字符串的用例 例只执行含有abc的测试用例: pytest -sv ./dir -k "abc"

4.测试用例执行循序
使用mark标记

image.png