pytest失败重跑插件: pytest-rerunfailures使用的坑

在pycharm中,使用pytest-rerunfailures的过程中,发现在pytest.main(['-v', '--reruns', '3']) 或 pytest.main(['-v', '--reruns = 3'])中,使用参数配置重试,没有效果,但是在控制台中,使用pytest -v --reruns 3命令可以正常运行,使用配置文件pytest.ini文件也可以,使用装饰器@pytest.mark.flaky(reruns=3, reruns_delay=2),也可以正常重试.暂时不知道是什么原因,知道的请留言(^_^)

使用pytest.main() 或 使用装饰器@pytest.mark.flaky(reruns=3, reruns_delay=2)#

复制# test_case1.py
import pytest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By  # 导入 By 类


@pytest.fixture(scope="function")
def driver():
    chromedriver_path = r"./chromedriver.exe"
    service = Service(chromedriver_path)
    chrome_options = Options()  # 创建 ChromeOptions 对象
    driver = webdriver.Chrome(service=service, options=chrome_options)
    yield driver
    driver.quit()


# @pytest.mark.flaky(reruns=3, reruns_delay=2)
def test_example(driver):
    try:
        driver.get("https://cn.bing.com/")
        element = driver.find_element(By.ID, "est_cnn")  # 使用 find_element 和 By.ID
        assert element.is_displayed()  # 检查元素是否显示
    except NoSuchElementException as e:
        print(f"Element not found: {e}")
        raise e  # 抛出异常以触发 pytest 的重试机制


if __name__ == "__main__":
    # pytest.main(['-v', '--reruns', '3'])
    pytest.main(['-v'])  # 使用命令行参数指定重试次数

使用配置文件pytest.ini文件#

复制# pytest.ini
[pytest]
addopts = -v --reruns 3

使用控制台命令pytest -v --reruns 3#

复制pytest -v --reruns 3  
posted @   DuKe渡客  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2021-03-04 面试题-python 什么是闭包(closure)?
2019-03-04 JSON类型解析
点击右上角即可分享
微信分享提示
主题色彩