pytest-rerunfailures插件如何实现失败测试的重试功能?

2026-05-21 21:201阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计476个文字,预计阅读时间需要2分钟。

pytest-rerunfailures插件如何实现失败测试的重试功能?

首先安装失败重试插件:`pip install pytest-rerunfailures`

有三种方式来使用失败重试:

第一种:在测试方法上使用装饰器 `@pytest.mark.flaky()` 装饰器python@ pytest.mark.flaky()def test_example(): assert 1 + 1==2

第二种:在测试方法上使用装饰器 `@pytest.mark.flaky(reruns=1)` 装饰器python@ pytest.mark.flaky(reruns=1)def test_example(): assert 1 + 1==2

第三种:在 `pytest` 命令行参数中使用 `--reruns` 和 `--reruns-delay` 选项shellpytest --reruns 1 --reruns-delay 5

首先安装失败重试插件:

pip install pytest-rerunfailures

有三种方式来使用失败重试

第一种:在测试方法上使用@pytest.mark.flsky()装饰器

@pytest.mark.flaky(reruns=1) #失败重跑一次
@pytest.mark.flaky(reruns=1, reruns_delay=2) #失败重跑一次,在每次开跑前会等到2s

一个简单的例子如下:

#投资成功
@pytest.mark.flaky(reruns=1)
def test_invest_success(self, common_driver):
WelcomePage(common_driver).swipe_screen()
WelcomePage(common_driver).click_experience_now()
LoginPage(common_driver).click_register_login()
LoginPage(common_driver).input_phone(login_success_data["phone"])
LoginPage(common_driver).input_pwd(login_success_data["pwd"])
IndexPage(common_driver).click_later()
InvestPage(common_driver).enter_invest()
before_invest_money = InvestPage(common_driver).get_user_left_money()
InvestPage(common_driver).input_invest_money(invest_success_data["money"])
InvestPage(common_driver).invest_now()
InvestPage(common_driver).click_confirm()
InvestPage(common_driver).click_back()
IndexPage(common_driver).click_me()
after_invest_money = UserInfoPage(common_driver).get_user_left_money()
assert float(before_invest_money) == float(after_invest_money) + invest_success_data["check"]

第二种:在命令行使用

pytest test_invest.py::TestInvest::test_invest_success --reruns 1 #失败重跑一次
pytest test_invest.py::TestInvest::test_invest_success --reruns 1 --reruns-delay 2 #失败重跑一次,在每次开跑前会等到2s

一个简单的例子如下:

第三种:在main.py中使用

pytest-rerunfailures插件如何实现失败测试的重试功能?

#失败重跑一次
pytest.main([
"--reruns=1",
"-m", "fail",
"--junitxml", f"{test_reports}/autotest_report_{cur_time}.xml",
"--html", f"{test_reports}/autotest_report_{cur_time}.html"]
)



#失败重跑一次,在每次开跑前会等待2s
pytest.main([
"--reruns=1",
"--reruns-delay=2",
"-m", "fail",
"--junitxml", f"{test_reports}/autotest_report_{cur_time}.xml",
"--html", f"{test_reports}/autotest_report_{cur_time}.html"]
)


本文共计476个文字,预计阅读时间需要2分钟。

pytest-rerunfailures插件如何实现失败测试的重试功能?

首先安装失败重试插件:`pip install pytest-rerunfailures`

有三种方式来使用失败重试:

第一种:在测试方法上使用装饰器 `@pytest.mark.flaky()` 装饰器python@ pytest.mark.flaky()def test_example(): assert 1 + 1==2

第二种:在测试方法上使用装饰器 `@pytest.mark.flaky(reruns=1)` 装饰器python@ pytest.mark.flaky(reruns=1)def test_example(): assert 1 + 1==2

第三种:在 `pytest` 命令行参数中使用 `--reruns` 和 `--reruns-delay` 选项shellpytest --reruns 1 --reruns-delay 5

首先安装失败重试插件:

pip install pytest-rerunfailures

有三种方式来使用失败重试

第一种:在测试方法上使用@pytest.mark.flsky()装饰器

@pytest.mark.flaky(reruns=1) #失败重跑一次
@pytest.mark.flaky(reruns=1, reruns_delay=2) #失败重跑一次,在每次开跑前会等到2s

一个简单的例子如下:

#投资成功
@pytest.mark.flaky(reruns=1)
def test_invest_success(self, common_driver):
WelcomePage(common_driver).swipe_screen()
WelcomePage(common_driver).click_experience_now()
LoginPage(common_driver).click_register_login()
LoginPage(common_driver).input_phone(login_success_data["phone"])
LoginPage(common_driver).input_pwd(login_success_data["pwd"])
IndexPage(common_driver).click_later()
InvestPage(common_driver).enter_invest()
before_invest_money = InvestPage(common_driver).get_user_left_money()
InvestPage(common_driver).input_invest_money(invest_success_data["money"])
InvestPage(common_driver).invest_now()
InvestPage(common_driver).click_confirm()
InvestPage(common_driver).click_back()
IndexPage(common_driver).click_me()
after_invest_money = UserInfoPage(common_driver).get_user_left_money()
assert float(before_invest_money) == float(after_invest_money) + invest_success_data["check"]

第二种:在命令行使用

pytest test_invest.py::TestInvest::test_invest_success --reruns 1 #失败重跑一次
pytest test_invest.py::TestInvest::test_invest_success --reruns 1 --reruns-delay 2 #失败重跑一次,在每次开跑前会等到2s

一个简单的例子如下:

第三种:在main.py中使用

pytest-rerunfailures插件如何实现失败测试的重试功能?

#失败重跑一次
pytest.main([
"--reruns=1",
"-m", "fail",
"--junitxml", f"{test_reports}/autotest_report_{cur_time}.xml",
"--html", f"{test_reports}/autotest_report_{cur_time}.html"]
)



#失败重跑一次,在每次开跑前会等待2s
pytest.main([
"--reruns=1",
"--reruns-delay=2",
"-m", "fail",
"--junitxml", f"{test_reports}/autotest_report_{cur_time}.xml",
"--html", f"{test_reports}/autotest_report_{cur_time}.html"]
)