其他分享
首页 > 其他分享> > pytest学习系列_插件之失败重试

pytest学习系列_插件之失败重试

作者:互联网

一、前言

  我们在做接口自动化的时候,经常遇到网络波动而引起的用例失败问题,比如超时失败,我们就需要一种自定义对所有的由于网络波动而造成失败的用例的重试机制,以更好的帮助我们的自动化工程提高稳定性。这不是用例或者是功能的问题,是第三方因素引起的。pytest的第三方插件pytest-rerunfailures给我们提供一个很好的解决方案。

二、安装

  pip install pytest-rerunfailures

 

三、使用方法

 

  1.命令行参数形式------命令行参数是针对于所有的测试用例(使用的比较多)

  比如:pytest --reruns 2  表示:运行失败的用例可以重新运行2次

  --only-rerun AssertionError --only-rerun ValueError     还可以增加只针对断言或者是值错误的类型

  比如:pytest --reruns 2 --reruns-delay 5  表示:(译:瑞软四、地类)运行失败的用例可以重新运行2次,第一次和第二次的间隔时间为5秒钟

 

  2.使用装饰器------装饰器是针对于单个的测试用例(使用的比较少)
  @pytest.mark.flaky(reruns=重试次数, reruns_delay=次数之间的延时设置(单位:秒))

 

四、代码示例

  1、代码

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
@author:durant.zeng
@Description:描述
@file:test_rerun.py
@time:2020/12/11
"""

import requests

def test_taobao():
    url = "https://taobao.com"
    r = requests.post(url=url,timeout=0.80)
    print(r.elapsed.total_seconds())

  上面的请求淘宝,客户端主动设置一个超时时间,那么执行结果就随机有两种情况,就比较好的模拟出线上环境由于网络波动而引起的用例概率性失败问题

用例成功

(CloudStorage) D:\learn\IOT\CloudStorage\unit>pytest
Test session starts (platform: win32, Python 3.7.2, pytest 6.0.1, pytest-sugar 0.9.4)
rootdir: D:\learn\IOT\CloudStorage\unit
plugins: Faker-4.1.3, rerunfailures-9.1.1, sugar-0.9.4, tavern-1.11.1, allure-pytest-2.8.18, base-url-1.4.2, forked-1.3.0, xdist-2.1.0
collecting ... 
 test_rerun.py ✓                                                                                                                             100% ████
██████
==================================================================== warnings summary =====================================================================
c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7
  c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7: DeprecationWarning: the imp module is deprecated in favour of import
lib; see the module's documentation for alternative uses
    import imp

-- Docs: https://docs.pytest.org/en/stable/warnings.html

Results (0.87s):
       1 passed

用例失败

==================================================================== warnings summary =====================================================================
c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7
  c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7: DeprecationWarning: the imp module is deprecated in favour of import
lib; see the module's documentation for alternative uses
    import imp

-- Docs: https://docs.pytest.org/en/stable/warnings.html
================================================================= short test summary info =================================================================
FAILED test_rerun.py::test_taobao - requests.exceptions.ConnectionError: ('Connection aborted.', OSError(0, 'Error'))

Results (0.97s):
       1 failed
         - test_rerun.py:12 test_taobao

  2、使用插件的命令行参数

  pytest --reruns 2

(CloudStorage) D:\learn\IOT\CloudStorage\unit>pytest --reruns 2
Test session starts (platform: win32, Python 3.7.2, pytest 6.0.1, pytest-sugar 0.9.4)
rootdir: D:\learn\IOT\CloudStorage\unit
plugins: Faker-4.1.3, rerunfailures-9.1.1, sugar-0.9.4, tavern-1.11.1, allure-pytest-2.8.18, base-url-1.4.2, forked-1.3.0, xdist-2.1.0
collecting ... 
 test_rerun.py ✓                                                                                                                             100% ████
██████
==================================================================== warnings summary =====================================================================
c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7
  c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7: DeprecationWarning: the imp module is deprecated in favour of import
lib; see the module's documentation for alternative uses
    import imp

-- Docs: https://docs.pytest.org/en/stable/warnings.html

Results (0.96s):
       1 passed

(CloudStorage) D:\learn\IOT\CloudStorage\unit>pytest --reruns 2
Test session starts (platform: win32, Python 3.7.2, pytest 6.0.1, pytest-sugar 0.9.4)
rootdir: D:\learn\IOT\CloudStorage\unit
plugins: Faker-4.1.3, rerunfailures-9.1.1, sugar-0.9.4, tavern-1.11.1, allure-pytest-2.8.18, base-url-1.4.2, forked-1.3.0, xdist-2.1.0
collecting ... 
 test_rerun.py R✓                                                                                                                            100% ████
██████
==================================================================== warnings summary =====================================================================
c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7
  c:\users\durant.zeng\.virtualenvs\cloudstorage\lib\site-packages\pykwalify\core.py:7: DeprecationWarning: the imp module is deprecated in favour of import
lib; see the module's documentation for alternative uses
    import imp

-- Docs: https://docs.pytest.org/en/stable/warnings.html

Results (1.39s):
       1 passed
       1 rerun

  上面的结果可以看出,用例整体执行通过,重试了1次

 

标签:插件,reruns,lib,--,py,重试,pytest,test
来源: https://www.cnblogs.com/Durant0420/p/14106310.html