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

浙公网安备 33010602011771号