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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2021-03-04 面试题-python 什么是闭包(closure)?
2019-03-04 JSON类型解析