有人可以告诉我如何使用 Python 在 Behave 中再次运行失败的测试吗?
如果失败,我想自动重新运行失败的测试用例。
行为库实际上有一个RerunFormatter
可以帮助您重新运行之前测试运行的失败场景。它会创建一个包含所有失败场景的文本文件,例如:
# -- file:rerun.features
# RERUN: Failing scenarios during last test run.
features/auth.feature:10
features/auth.feature:42
features/notifications.feature:67
要使用RerunFormatter
您需要做的所有事情,只需将其放入您的行为配置文件(behave.ini)中:
# -- file:behave.ini
[behave]
format = rerun
outfiles = rerun_failing.features
要重新运行失败的场景,请使用以下命令:
behave @rerun_failing.features
我知道这是后来的答案,但它可以帮助其他人。
还有另一种方法也可以提供帮助,它在 environment.py 文件下实现它,您可以通过特定标签进行重试。
提供支持功能以在接受失败之前多次重试场景。当您在不可靠的服务器/网络基础架构中使用行为测试时,此功能会很有帮助。
例如,我在 CI 上运行标签“@smoke_test”,所以我选择这个标签来修补重试条件。
首先,在您的 environment.py 上导入以下内容:
# -- file: environment.py
from behave.contrib.scenario_autoretry import patch_scenario_with_autoretry
然后添加方法:
# -- file:environment.py
#
def before_feature(context, feature):
for scenario in feature.scenarios:
if "smoke_test" in scenario.effective_tags:
patch_scenario_with_autoretry(scenario, max_attempts=3)
*max_attempts 默认设置为 3。我只是在此处进行了描述,以明确说明您实际上可以设置您想要的重试次数。