美文网首页pytest
python接口自动化-pytest-重试测试

python接口自动化-pytest-重试测试

作者: 疯子李 | 来源:发表于2022-07-05 22:15 被阅读0次

背景

编写自动化过程中,经常会遇到服务不稳定情况,只执行一次结果可能说明不了问题,这个时候引入重试机制,能大幅提高用例成功率,但是也会增加执行时间。

一、用法

  • 1、安装
    pip install pytest-rerunfailures

  • 2、装饰器用法
    @pytest.mark.flaky(reruns=2, reruns_delay=5)
    -代表重试2次,每次间隔5s

  • 3、命令行用法:
    命令:pytest --reruns 重试次数 --reruns-delay 重试间隔
    比如:pytest --reruns 2 --reruns-delay 5  
    (表示:运行失败的用例可以重新运行2次,第一次和第二次的间隔时间为5秒钟)

二、实战

1、装饰器用法

-装饰器-作用域-function


装饰器用法-作用域-函数

-装饰器-作用域-class


装饰器用法-作用域-类

2、命令行用法

  • 命令行-作用域-session
    pytest test_case/api/ --reruns 2 --reruns-delay 5

  • 命令行-作用域-module
    pytest test_case/api/xxx.py --reruns 2 --reruns-delay 5

  • 命令行-作用域-class
    pytest test_case/api/xxx.py::xx类 --reruns 2 --reruns-delay 5

  • 命令行-作用域-function
    pytest test_case/api/xxx.py::xx类::xx方法 --reruns 2 --reruns-delay 5

相关文章

网友评论

    本文标题:python接口自动化-pytest-重试测试

    本文链接:https://www.haomeiwen.com/subject/cxnxbrtx.html