如果你还想从头学起Pytest,可以看看这个系列的文章哦!
https://www.cnblogs.com/poloyy/category/1690628.html
环境前提
以下先决条件才能使用pytest-rerunfailures
- Python 3.5, 最高 3.8, or PyPy3
- pytest 5.0或更高版本
安装插件
pip3 install pytest-rerunfailures -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
提前了解重点
命令行参数:--reruns n(重新运行次数),--reruns-delay m(等待运行秒数)
装饰器参数:reruns=n(重新运行次数),reruns_delay=m(等待运行秒数)
重新运行所有失败的用例
要重新运行所有测试失败,使用 --reruns 命令行选项,并指定要运行测试的最大次数:
pytest --reruns 5 -s
知识点
运行失败的fixture或setup_class也将重新执行
添加重新运行的延时
要在两次重试之间增加延迟时间,使用 --reruns-delay 命令行选项,指定下次测试重新开始之前等待的秒数
pytest --reruns 5 --reruns-delay 10 -s
重新运行指定的测试用例
要将单个测试用例添加flaky装饰器 @pytest.mark.flaky(reruns=5) ,并在测试失败时自动重新运行,需要指定最大重新运行的次数
小栗子
import pytest @pytest.mark.flaky(reruns=5) def test_example(): import random assert random.choice([True, False, False])
执行结果
collecting ... collected 1 item 11_reruns.py::test_example RERUN [100%] 11_reruns.py::test_example PASSED [100%] ========================= 1 passed, 1 rerun in 0.05s ==========================
同样的,这个也可以指定重新运行的等待时间
@pytest.mark.flaky(reruns=5, reruns_delay=2) def test_example(): import random assert random.choice([True, False, False])
注意事项
如果指定了用例的重新运行次数,则在命令行添加--reruns对这些用例是不会生效的
兼容性问题
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。