【测试框架篇】单元测试框架pytest(3):用例执行参数详解

一、前言

上一篇内容介绍了用例编写的规则以及执行用例,执行用例时我们发现有些print输出内容,结果没有给我们展示,这是因为什么原因呢?接下来我们会针对这些问题进行阐述。

二、参数大全

我们可以在cmd中通过输入 pytest -h 或者pytest --help 来查看帮助里面打印出来的都有哪些参数:

1、参数打印

  • general:
  • Reporting:
  • pytest-warnings:
  • collection:
  • test session debugging and configuration:
  • logging:
  • reporting:
  • pytest-metadata:
  • testreport:
  • distributed and subprocess testing:
  • pytest ini-options in the first pytest.ini|tox.ini|setup.cfg|pyproject.toml file found:
  • Environment variables:

2、常用的参数

1)pytest - s

-s:执行用例,携带详细信息,比如打印的print内容

通过cmd进行执行,发现已经详细内容展示出来了

2)pytest -x

-x:遇到错误的用例,立即退出执行,并输出结果

从上图可以看到一共3条用例,执行了2条,其中执行到第2条的时候就已经程序就退出了。

3)pytest -k

-k:表示加需要执行的用例项,-k后面可以是测试函数名,也可以是类名,也可以是模块名,以及目录名称

也可以通过加入not来不执行一些用例,这里的not要和用例名放在一起用引号

4)pytest xxx.py

::类::用例:可以通过这种形式进行指定用例执行

5)pytest -m

-m:执行被标记的用例

标记需要用到pytest中的装饰器 @pytest.mark.标记名称

python 复制代码
import pytest
class Test_01:
    
    @pytest.mark.anjing
    def test_001(self):
        print('Test_01下的用例001')
        assert  1 == 1

    def test_002(self):
        print('Test_01下的用例002')
        assert  1 == 2
        
    @pytest.mark.anjing
    def test_003(self):
        print('Test_01下的用例003')
        assert 3 == 3

if __name__ == '__main__':
    pytest.main()

6)pytest -v

-v:表示查看详细的报告内容

7)pytest -q

-q:表示简则的测试报告,运行成功通过"."表示,运行失败通过"F"表示

8)pytest --collect-only

--collect-only:表示把待执行的用例全部展示出来。

9)pytest --maxfail=n

--maxfail=n:其中n表示失败的次数,运行失败n次后,进行停止测试

python 复制代码
# coding:utf-8
class Test:
    def test_01(self):
        print('这是用例01')
        assert 1 == 1

    def test_02(self):
        print('这是用例02')
        assert 1 == 2

    def test_03(self):
        print('这是用例03')
        assert 3 == 4

    def test_04(self):
        print('这是用例04')
        assert 4 == 4

10)pytest --lf

--lf:只执行上次失败的用例

11)pytest --tb

--tb=no:不展示用例失败的错误详情

--tb=line:展示用例失败的代码具体行数。下图结果是在11行

--tb=short:展示更加详细的错误信息

12)pytest --durations

--duration=N:表示把最耗时间的用例展示出来,N表示最慢的N个

安静的代码运行都很快,把其中一个代码加上睡眠,从图中可以看出来,test_01文件中的test_03运行最慢。

上面简单描述了常用的一些参数,pytest插件多样,大家有想了解的可以参考官方文档。我后面也会有针对性的进行补充,敬请期待哈!

相关推荐
2601_962387822 天前
web自动化测试实战教程【selenium/unittest/pytest】【共193课
selenium·pytest·devops·web自动化测试·unittest
weixin_440730502 天前
使用pytest中方法控制执行步骤(test_begin.py、test_end.py,@pytest.mark.run(order=1))
开发语言·python·pytest
2601_962297252 天前
Python、Pytest、Allure、Selenium和Jenkins实现自动化测试集成实例
python·selenium·jenkins·pytest·allure
2601_962077603 天前
从零搭建Python接口自动化测试框架:pytest+requests实战指南
python·pytest·接口自动化·requests·框架搭建
weixin_440730503 天前
pytest结合allure生成html测试报告(step、story、severity、screenshot)
前端·html·pytest·allure报告
拜托多多指教3 天前
【Pytest框架学习】分层架构
python·pytest
2601_962299883 天前
python脚本如何单元测试
python·单元测试·pytest·unittest·mock对象
Tizzy JJ4 天前
Python + pytest 接口自动化测试框架实战:从零搭建企业级项目骨架
开发语言·python·pytest
牛油果子哥q4 天前
C++大型项目工程精讲:CMake完整实战、静态库&动态库、模块化拆分、单元测试、gdb调试、性能工具、工程踩坑全解
开发语言·c++·单元测试
Tizzy JJ4 天前
接口自动化测试实战:如何围绕真实业务场景设计高质量用例
python·自动化·pytest