• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Python Pytest 用例管理常用功能

武飞扬头像
天青色是₩
帮助1

pytest功能很强大,但是对用例的管理大多是用pytest的插件来实现,以下介绍几个常用的用例管理插件,有些功能使用前必须安装对应插件!!!

一、用例的执行顺序

pytest的默认执行顺序是从上往下执行用例,若涉及到执行顺序的变更,可以使用这个插件,通过pytest的装饰器来使用该插件,优先执行有装饰器的用例,然后再执行没加装饰器的用例。

安装:

     pip install pytest-ordering

使用:在用例上添加@pytest.mark.run(order=1) 装饰器来使用

  1.  
    @pytest.mark.run(order=3)
  2.  
    def test_a():
  3.  
    print('a')
  4.  
     
  5.  
    @pytest.mark.run(order=1)
  6.  
    def test_b():
  7.  
    print('b')
  8.  
     
  9.  
    @pytest.mark.run(order=2)
  10.  
    def test_c():
  11.  
    print('c')

默认的执行顺序是 a b c,加了装饰器后执行顺序已经改变,变成了 b c a ,order=1数字越小越先执行

  1.  
    test_demo1.py::test_b b
  2.  
    PASSED
  3.  
    test_demo1.py::test_c c
  4.  
    PASSED
  5.  
    test_demo1.py::test_a a
  6.  
    PASSED
  7.  
     
  8.  
    ============================== 3 passed in 0.03s ==============================

二、跳过用例

当用例执行的粒度不需要这么细时,执行时可以跳过部分用例,可分为:

无条件跳过 : pytest.mark.skip(reason='跳过原因')  或  pytest.mark.skip()  '跳过原因'  可不加

有条件跳过 : pytest.mark.skipif(version>2, reason='跳过原因')  '跳过原因' 必须加

以上两个跳过都不需要安装插件,可直接使用。

无条件跳过:

  1.  
    @pytest.mark.run(order=3)
  2.  
    def test_a():
  3.  
    print('a')
  4.  
     
  5.  
    @pytest.mark.skip(reason="粒度不需要")
  6.  
    def test_b():
  7.  
    print('b')
  8.  
     
  9.  
    @pytest.mark.skip()
  10.  
    def test_c():
  11.  
    print('c')

这里执行时,b 和 c 都会被跳过

  1.  
    test_demo1.py::test_a a
  2.  
    PASSED
  3.  
    test_demo1.py::test_b SKIPPED (粒度不需要)
  4.  
    test_demo1.py::test_c SKIPPED (unconditional skip)
  5.  
     
  6.  
    ======================== 1 passed, 2 skipped in 0.06s =========================

有条件跳过:需要先有条件,例如有变量 version=3

  1.  
     
  2.  
    version = 3
  3.  
     
  4.  
     
  5.  
    @pytest.mark.skipif(version>4, reason="版本太低")
  6.  
    def test_a():
  7.  
    print('a')
  8.  
     
  9.  
     
  10.  
    @pytest.mark.skipif(version>2, reason="版本太低")
  11.  
    def test_b():
  12.  
    print('b')
  13.  
     
  14.  
     
  15.  
    @pytest.mark.skipif(version>1, reason="版本太低")
  16.  
    def test_c():
  17.  
    print('c')
学新通

这里判断 version>n 的结果为 true时,则会跳过这个用例不会执行,否则不跳过,b c 都被跳过

  1.  
    test_demo1.py::test_a a
  2.  
    PASSED
  3.  
    test_demo1.py::test_b SKIPPED (版本太低)
  4.  
    test_demo1.py::test_c SKIPPED (版本太低)
  5.  
     
  6.  
    ======================== 1 passed, 2 skipped in 0.03s =========================

三、用例失败重

当用例执行失败时,会自动重试,主要在web自动化中用的较多,对于环境不稳定的接口自动化也可以使用。

安装:

    pip install pytest-rerunfailures

使用:需要在执行命令或者 pytest.ini 文件中加参数  --reruns=2,这里的2是重试的次数

  1.  
    import pytest
  2.  
     
  3.  
     
  4.  
    if __name__ == '__main__':
  5.  
     
  6.  
    pytest.main(['-vs', '--reruns=2'])

pytest.ini 文件是pytest的执行的配置文件,不管是主函数执行还命令行执行都会读取这个文件,详细了解请跳转至:http://t.csdn.cn/gYEWH

  1.  
    [pytest]
  2.  
     
  3.  
    addopts = -vs --reruns 2
  4.  
    testpaths = test_demo1.py
  5.  
     
  6.  
    python_files = test_*
  7.  
    python_classes = Test*
  8.  
    python_functions = test_*
  9.  
     
  10.  
    markers =
  11.  
    smoke: maoyan

 这里运行时 test_c 报错,可以看到 RERUN 重试了两次

  1.  
    test_demo1.py::test_a a
  2.  
    PASSED
  3.  
    test_demo1.py::test_b b
  4.  
    PASSED
  5.  
    test_demo1.py::test_c RERUN
  6.  
    test_demo1.py::test_c RERUN
  7.  
    test_demo1.py::test_c FAILED
  8.  
     
  9.  
    ================================== FAILURES ===================================
  10.  
    ___________________________________ test_c ____________________________________
  11.  
     
  12.  
    def test_c():
  13.  
    > raise Exception('error')
  14.  
    E Exception: error
  15.  
     
  16.  
    test_demo1.py:17: Exception
  17.  
    =========================== short test summary info ===========================
  18.  
    FAILED test_demo1.py::test_c - Exception: error
  19.  
    ==================== 1 failed, 2 passed, 2 rerun in 0.25s =====================
  20.  
     
学新通

四、用例多线程执行

当用例很多时,可以使用多线程执行来减少执行时间,提高执行效率.。

安装:

    pip install pytest-xdist

 使用:只需要在执行命令或者 pytest.ini 文件中加参数  -n=2 ,这里的2是指线程数

  1.  
    import pytest
  2.  
     
  3.  
     
  4.  
    if __name__ == '__main__':
  5.  
     
  6.  
    pytest.main(['-vs', '-n=2'])

  pytest.ini 文件是pytest的执行的配置文件,不管是主函数执行还命令行执行都会读取这个文件,详细了解请跳转至:http://t.csdn.cn/gYEWH

  1.  
    [pytest]
  2.  
     
  3.  
    addopts = -vs -n 2
  4.  
    testpaths = test_demo1.py
  5.  
     
  6.  
    python_files = test_*
  7.  
    python_classes = Test*
  8.  
    python_functions = test_*
  9.  
     
  10.  
    markers =
  11.  
    smoke: maoyan

 执行后可以看到这里分成两个线程执行,gw0  gw1 ,a c 是gw0线程执行,b 是gw1线程执行,若两条用例涉及到上下游传参,最好走同一条线程。

  1.  
    test_demo1.py::test_a
  2.  
    test_demo1.py::test_b
  3.  
    [gw1] PASSED test_demo1.py::test_b
  4.  
    [gw0] PASSED test_demo1.py::test_a
  5.  
    test_demo1.py::test_c
  6.  
    [gw0] PASSED test_demo1.py::test_c
  7.  
     
  8.  
    ============================== 3 passed in 3.08s ==============================

五、用例加标记,指定标记用例执行

给用例加标记,就可以运行指定标记的用例,比如冒烟用例。

配置:不需要下载插件,在pytest.ini 文件中配置标签,然后运行时加参数 -m smoke

参数可以加多个,比如 smoke:maoyan    、other:qita,标签名自定义,最好都是英文

  1.  
    [pytest]
  2.  
     
  3.  
    addopts = -vs -n 2
  4.  
    testpaths = test_demo1.py
  5.  
     
  6.  
    python_files = test_*
  7.  
    python_classes = Test*
  8.  
    python_functions = test_*
  9.  
     
  10.  
    markers =
  11.  
    smoke: maoyan

使用:对应用例上面加装饰器 @pytest.mark.smoke ,运行时加参数  -m 标签名

同时也可以一次选择多个标签一起运行,例如:-m "smoke or other", 运行时会选中标签有

@pytest.mark.smoke  和 @pytest.mark.other 的标签用例会被执行,其他的用例不会执行

  1.  
    import pytest
  2.  
     
  3.  
     
  4.  
    def test_a():
  5.  
    print('a')
  6.  
     
  7.  
    @pytest.mark.smoke
  8.  
    def test_b():
  9.  
    print('b')
  10.  
     
  11.  
     
  12.  
    def test_c():
  13.  
    print('c')

 这里主函数添加参数或者 pytest.ini 文件添加参数,只需要任选其一就行,不需要两种方式都加参数

  1.  
    import pytest
  2.  
     
  3.  
     
  4.  
    if __name__ == '__main__':
  5.  
     
  6.  
    pytest.main(['-vs', '-m=smoke'])
  1.  
    [pytest]
  2.  
     
  3.  
    addopts = -vs -m smoke
  4.  
    testpaths = test_demo1.py
  5.  
     
  6.  
    python_files = test_*
  7.  
    python_classes = Test*
  8.  
    python_functions = test_*
  9.  
     
  10.  
    markers =
  11.  
    smoke: maoyan

运行之后可以看到只有加了标签的用例才会被执行,b 加了标签,只执行 b

  1.  
    test_demo1.py::test_b b
  2.  
    PASSED
  3.  
     
  4.  
    ======================= 1 passed, 2 deselected in 0.04s =======================

注: 这里主函数添加参数或者 pytest.ini 文件添加参数,只需要任选其一就行,不需要两种方式都加参数,想要了解 pytest.ini 配置文件的, 请跳转至:http://t.csdn.cn/gYEWH

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjaaae
系列文章
更多 icon
同类精品
更多 icon
继续加载