1.测试用例运行方法

2.测试用例运行方法 - 图1

1.主函数模式

  1. 运行所有:pytest.main()
  2. 指定模块(py): pytest.main([‘-vs’],‘test_xxx.py’)
  3. 指定目录: pytest.main([‘-vs‘],‘目录路径’)
  4. 指定函数方法:pytest.main[‘目录/test_xxx.py::Test_xxx::test01’]

2.命令行(shell)模式

  1. 运行所有:pytest
  2. 指定模块(py): pytest -vs test_xxx.py
  3. 指定目录: pytest -vs 目录路径
  4. 指定函数方法:pytest -vs 目录/test_xxx.py::Test_xxx::test01

    3.pytest.ini文件的配置

    pytest.ini是pytest的主配置文件。

    1.pytest.ini的作用

    可根据pytest.ini文件中的配置规则来运行运行pytest的TestCase【pytest的主配置文件,可以改变pytest的默认行为

2.参数详解

1.常用参数

序号 参数 作用 备注
1 -s 输出调试信息(包括打印信息)
2 -v/-vv 输出更详细的信息
3 -vs 输出调试及详细信息
4 -k 根据“关键字”进行执行
5 -m 根据“marker分组(需在pytest.ini配置)”进行执行
6 -x 遇到”fail Testcase”则停止执行,不会往后执行
7 —maxfail 设置可以失败的次数【超过最大次数,则停止执行】
8 —lf(last failed) 定位到“最后一个failed TestCase”并重新执行
9 —ff(failed first) 遇到“failed Testcase”继续执行
10 —tb=no 屏蔽所有回溯信息
11 —tb=line 定位fail的位置
12 —tb=short 显示的回溯信息比前两种更详细
13 —tb=long 最为详细的回溯信息
14 —tb=auto 【默认值】若有多个failed的testcase,则只显示“第1个与最后1个”
15 —tb=native 只输出python标准库的回溯信息,不显示其他信息
不常用
16 -l 显示“failed Testcase”的“局部变量及变量值”
17 —collect-only 表示把待执行的用例全部展示出来
18 —duration=N 显示最慢的N个阶段(耗时最长在最前面
19 -rs(混合使用) 查看具体“跳过的原因”【注意:不能单独使用一个“-r”
20 —setup-show 查看fixture的执行顺序

1.pytest的结果状态

状态 缩写 说明 查看指定类型的测试结果
PASSED . 通过 不适用
Failed f 失败 pytest -rf
Skipped s 跳过 pytest -rs
xfail x 预期测试失败,并且确实失败 pytest -rx
XPASS X 预期测试失败,但实际上 pytest -rX

2.区别

1.tb=no与tb=line的结果区别

tb_no与line的结果区别.png

2.tb=short与tb=line的结果区别

tb_short与line的结果却别.png

3.tb=long与tb=short的结果区别

tb_long与short的结果区别.png

3.【扩展】

1.-n 多线程运行

-n :支持多线程或分布式运行TestCase 【PS:务必保证已安装pytest-xdist插件】【参考

  1. #[-n 数字] 或 [-n auto-> 可以自动检测到系统的CPU核数]
  2. # 主函数模式
  3. pytest.main(['-vs','./hello1_testcase','--html=./report/Report.html','-n=2'])
  4. # 命令行模式
  5. pytest ./hello1_testcase/ --html=./report/hello.html -n 2

image.png

2.—html: 生成pytest-html测试报告

【PS:务必保证已安装pytest-html插件】
image.png

  1. # 主函数模式
  2. pytest.main(['-vs','./hello1_testcase','--html=./report/Report.html','-n=2'])
  3. # 命令行模式
  4. pytest ./hello1_testcase/ --html=./report/hello.html -n 2

3.-p no:sugar terminal关闭sugar

  1. pytest -rf test_param2.py::test_hello2 -p no:sugar

no_sugar.png