BDD - Python Behave Retry 机制

BDD - Python Behave Retry 机制

  • 引言
  • [Behave Retry](#Behave Retry)
  • [Behave Retry 应用](#Behave Retry 应用)
    • [feature 文件](#feature 文件)
    • [创建 step 文件](#创建 step 文件)
    • Retry

引言

在日常运行测试用例,有时因为环境不稳定造成一些测试用例跑失败了,如果能将这些失败的测试用例再跑一遍,排除环境问题导致的不稳定,从而可以节省每天 triage 的时间。Behave 也有 retry 机制,今天就来了解一下吧。

想了解更多 Behave 相关的文章,欢迎阅读《Python BDD Behave 系列》,持续更新中。

Behave Retry

behave -f rerun 是 Behave 提供的一种输出格式(formatter),用于生成 "rerun" 格式的输出。-f rerun 选项可用于将测试失败的场景(Scenarios)输出到一个文件,然后可以使用这个文件来重新运行失败的测试。

以下是使用 behave -f rerun 的简单示例:

  1. 运行 Behave 并将失败的场景输出到 rerun 文件:
bash 复制代码
behave -f rerun -o failed_scenarios.txt

这个命令将运行测试并将失败的场景信息输出到 failed_scenarios.txt 文件中。生成的 rerun 文件包含了失败场景的标识符,如 features/my_feature.feature:3,这样 Behave 能够准确识别并重新运行这些场景。

  1. 重新运行失败的场景:
bash 复制代码
behave @failed_scenarios.txt

此命令将重新运行 failed_scenarios.txt 文件中列出的所有失败场景。

注意上面的命令有可能会执行失败因为 @ 符号异常,我们可以换另外一种方式执行,加上双引号""

bash 复制代码
behave "@failed_scenarios.txt"

Behave Retry 应用

举个简单的例子,项目结构如下:

feature 文件

创建 my_feature.feature 文件

python 复制代码
Feature: Example Feature

  Scenario: Failing Scenario
    Given I have a failing step
    When I perform an action
    Then I should see a result

创建 step 文件

创建 my_steps.py 文件

python 复制代码
from behave import given, when, then

@given('I have a failing step')
def step_given(context):
    assert False, "Simulating a failure in the Given step"

@when('I perform an action')
def step_when(context):
    print("When step executed")

@then('I should see a result')
def step_then(context):
    print("Then step executed")

Retry

运行 Behave 并生成 rerun 文件

执行测试用例: behave Features/Retry -f rerun -o failed_scenarios.txt

将跑失败的测试用例输出到 failed_scenarios.txt 文件中。

bash 复制代码
PS C:\Automation\Test\bdd> behave Features/Retry -f rerun -o failed_scenarios.txt

Failing scenarios:
  Features/Retry/my_feature.feature:3  Failing Scenario

0 features passed, 1 failed, 0 skipped
0 scenarios passed, 1 failed, 0 skipped
0 steps passed, 1 failed, 2 skipped, 0 undefined       
Took 0m0.000s

当前目录生成 failed_scenarios.txt,文件内容列出失败 Scenario 的路径及行数。

重新运行失败的场景

执行命令 behave @failed_scenarios.txt

抛异常了:The splatting operator '@' cannot be used to reference variables in an expression

bash 复制代码
PS C:\Automation\Test\bdd> behave @failed_scenarios.txt      
At line:1 char:8
+ behave @failed_scenarios.txt
+        ~~~~~~~~~~~~~~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@failed_scen 
arios' can be used only as an argument to a command. To reference variables in an expression use 
 '$failed_scenarios'.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : SplattingNotPermitted

换另外一种方式,将 @failed_scenarios.txt 用双引号包装一下。

执行命令 behave "@failed_scenarios.txt"

此命令将重新运行 failed_scenarios.txt 文件中列出的所有失败场景。在这个例子中,由于步骤定义中的 Given 步骤故意失败,Behave 将重试执行这个场景,并且你会看到类似于以下的输出:

bash 复制代码
PS C:\Automation\Test\bdd> behave "@failed_scenarios.txt"
Feature: Example Feature # Features/Retry/my_feature.feature:1

  Scenario: Failing Scenario    # Features/Retry/my_feature.feature:3
    Given I have a failing step # steps/my_steps.py:3
      Assertion Failed: Simulating a failure in the Given step

    When I perform an action    # None
    Then I should see a result  # None


Failing scenarios:
  Features/Retry/my_feature.feature:3  Failing Scenario

0 features passed, 1 failed, 0 skipped
0 scenarios passed, 1 failed, 0 skipped
0 steps passed, 1 failed, 2 skipped, 0 undefined
Took 0m0.000s
相关推荐
带娃的IT创业者7 分钟前
《Python实战进阶》专栏 No.3:Django 项目结构解析与入门DEMO
数据库·python·django
HealthScience36 分钟前
【异常错误】pycharm debug view变量的时候显示不全,中间会以...显示
ide·python·pycharm
豌豆花下猫2 小时前
Python 潮流周刊#90:uv 一周岁了,优缺点分析(摘要)
后端·python·ai
橘猫云计算机设计2 小时前
基于SSM的《计算机网络》题库管理系统(源码+lw+部署文档+讲解),源码可白嫖!
java·数据库·spring boot·后端·python·计算机网络·毕设
小伍_Five2 小时前
从0开始:OpenCV入门教程【图像处理基础】
图像处理·python·opencv
m0_748245342 小时前
python——Django 框架
开发语言·python·django
java1234_小锋3 小时前
一周学会Flask3 Python Web开发-客户端状态信息Cookie以及加密
前端·python·flask·flask3
B站计算机毕业设计超人3 小时前
计算机毕业设计Python+DeepSeek-R1高考推荐系统 高考分数线预测 大数据毕设(源码+LW文档+PPT+讲解)
大数据·python·机器学习·网络爬虫·课程设计·数据可视化·推荐算法
winfredzhang4 小时前
Python实战:Excel中文转拼音工具开发教程
python·安全·excel·汉字·pinyin·缩写
奔跑吧邓邓子4 小时前
【Python爬虫(34)】Python多进程编程:开启高效并行世界的钥匙
开发语言·爬虫·python·多进程