如何使用python重试失败的场景

Esw*_*war 4 python-behave

有人可以告诉我"我怎样才能在使用python的行为中再次运行失败的测试".

如果失败,我想自动重新运行失败的测试用例.

任何帮助赞赏.

谢谢

acc*_*aze 9

行为库实际上有一个RerunFormatter可以帮助您重新运行先前测试运行的失败方案.它会创建一个包含所有失败方案的文本文件,例如:

# -- file:rerun.features
# RERUN: Failing scenarios during last test run.
features/auth.feature:10
features/auth.feature:42
features/notifications.feature:67
Run Code Online (Sandbox Code Playgroud)

要使用RerunFormatter您需要做的就是将它放在您的行为配置文件(behave.ini)中:

# -- file:behave.ini
[behave]
format   = rerun
outfiles = rerun_failing.features
Run Code Online (Sandbox Code Playgroud)

要重新运行失败的方案,请使用以下命令:

behave @rerun_failing.features
Run Code Online (Sandbox Code Playgroud)


小智 9

我知道这是后来的答案,但它可以帮助其他人。

还有另一种方法也可以提供帮助,它在environment.py 文件下实现它,您可以通过特定标签进行重试。

提供支持功能,以便在接受失败之前多次重试场景。当您在不可靠的服务器/网络基础设施中使用行为测试时,此功能会很有帮助。

例如,我在 CI 上运行标签“@smoke_test”,因此我选择此标签来使用重试条件进行修补。

首先,在您的environment.py 上导入以下内容:

# -- file: environment.py
from behave.contrib.scenario_autoretry import patch_scenario_with_autoretry
Run Code Online (Sandbox Code Playgroud)

然后添加方法:

# -- 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)
Run Code Online (Sandbox Code Playgroud)

*max_attempts 默认设置为 3。我只是在那里描述,以明确您实际上可以设置您想要的重试次数。