pytest学习2-运行方式

pytest常用运行方式

  • 运行目录及子包下的所有用例: pytest 目录名
  • 运行指定模块所有用例: pytest test_reg.py
  • pytest test_reg.py::TestClass::test_method 运行指定模块指定类指定用例
  • 运行名称包含指定表达式的用例:-k 表达式(支持and or not),如pytest -k "test_a and test_b"
  • 运行指定标签(mark)的用例: -m 标签(支持and or not), 如pytest -m "apitest and level1"
  • 遇到失败后停止:-x/--exitfirst 首次失败后退出(可用于保留出错现场) --maxfails=3 3次失败后退出,如下:

    pytest -x # 第1次失败后停止
    pytest --maxfail=2 # 2次失败后停止

  • 执行上次失败的用例 --lf/--last-failed
  • 先执行上次失败的用例,再执行成功的用例 --ff/--failed-first
  • 只收集用例,不执行 --collect-only
  • 显示执行最慢的前N条用例:--durations=N
  • 并行执行: -n 2 (需安装pytest-xdist)

常用的参数:

其它常用参数

  • -q: 安静模式, 不输出环境信息
  • -v: 丰富信息模式, 输出更详细的用例执行信息
  • -s: 显示程序中的print/logging输出
  • pytest --resultlog=./log.txt 生成log
  • pytest --junitxml=./log.xml 生成xml报告
原文地址:https://www.cnblogs.com/qixc/p/12163535.html