本文主要是介绍pytest-rerunfailures:优化测试稳定性的失败重试工具,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 📢专注于分享软件测试干货内容,欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
- 📢交流讨论:欢迎加入我们一起学习!
- 📢资源分享:耗时200+小时精选的「软件测试」资料包
- 📢 软件测试学习教程推荐:火遍全网的《软件测试》教程
1、前言
什么是 pytest-rerunfailures ?
如何使用 pytest-rerunfailures ?
- 方式一
- <font size="3">pip install pytest-rerunfailures</font>
- <font size="3">
- import pytest
- @pytest.mark.flaky(reruns=3, reruns_delay=2)
- def test_case():
- assert 1 == 2</font>
- <font size="3">RERUN
- test_dir/test_demo.py::test_case RERUN
- test_dir/test_demo.py::test_case RERUN
- test_dir/test_demo.py::test_case FAILED</font>
- 方式二
- [size=3]pytest -s -v --reruns 3 --reruns-delay 2 test_demo.py::test_case[/size]
- [size=3]pytest.main(["-s", "-v", "--reruns", "3", "--reruns-delay", "2", "test_demo.py::test_case"])[/size]
- [size=3]def pytest_configure(config):
- # add flaky marker
- config.addinivalue_line(
- "markers",
- "flaky(reruns=1, reruns_delay=0): mark test to re-run up "
- "to 'reruns' times. Add a delay of 'reruns_delay' seconds "
- "between re-runs.",
- )
- ......[/size]
- pytest_configure(config) 是 pytest 的一个钩子函数,用于在 pytest 配置阶段对配置进行自定义操作。
- config.addinivalue_line() 是 pytest 的配置方法,用于向配置中添加新的配置项或配置信息。
- 在这段代码中,通过 config.addinivalue_line() 方法,插件向 pytest 的配置中加入了一行字符串。
- 这行字符串指定了标记名称为 "flaky",并使用参数 reruns 和 reruns_delay 来说明重试次数和延迟时间的默认值。
- 标记的含义是将被标记的测试用例重新运行最多 "reruns" 次,每次重试之间间隔 "reruns_delay" 秒。
- [size=3]def pytest_runtest_protocol(item, nextitem):
- """
- Run the test protocol.
- Note: when teardown fails, two reports are generated for the case, one for
- the test case and the other for the teardown error.
- """
- reruns = get_reruns_count(item)
- if reruns is None:
- # global setting is not specified, and this test is not marked with
- # flaky
- return
- # while this doesn't need to be run with every item, it will fail on the
- # first item if necessary
- check_options(item.session.config)
- delay = get_reruns_delay(item)
- parallel = not is_master(item.config)
- db = item.session.config.failures_db
- item.execution_count = db.get_test_failures(item.nodeid)
- db.set_test_reruns(item.nodeid, reruns)
- if item.execution_count > reruns:
- return True
- need_to_run = True
- while need_to_run:
- item.execution_count += 1
- item.ihook.pytest_runtest_logstart(nodeid=item.nodeid, location=item.location)
- reports = runtestprotocol(item, nextitem=nextitem, log=False)
- for report in reports: # 3 reports: setup, call, teardown
- report.rerun = item.execution_count - 1
- if _should_not_rerun(item, report, reruns):
- # last run or no failure detected, log normally
- item.ihook.pytest_runtest_logreport(report=report)
- else:
- # failure detected and reruns not exhausted, since i < reruns
- report.outcome = "rerun"
- time.sleep(delay)
- if not parallel or works_with_current_xdist():
- # will rerun test, log intermediate result
- item.ihook.pytest_runtest_logreport(report=report)
- # cleanin item's cashed results from any level of setups
- _remove_cached_results_from_failed_fixtures(item)
- _remove_failed_setup_state_from_session(item)
- break # trigger rerun
- else:
- need_to_run = False
- item.ihook.pytest_runtest_logfinish(nodeid=item.nodeid, location=item.location)
- return True[/size]
最后如果你想学习提升找不到资料,没人答疑解惑时,请及时加入群,里面有各种测试开发资料和技术可以一起交流哦:
感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助……
这篇关于pytest-rerunfailures:优化测试稳定性的失败重试工具的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!