BDD - Python Behave Tags 过滤

2023-12-23 18:29:27

引言

随着项目进展,QA 创建的 Behave 测试用例也会越来越多,我们通常者会加一些 Tags 来分类管理这些海量测试用例。当然我们在执行 Behave 测试用例时,也可以按 Tag 过滤执行,今天通过实例来了解一下 Behave 的 Tags 过滤。

实例

如果你还不太了解 Behave,请先阅读《BDD - Python Behave 入门》,这里我就不重复了,直接上代码吧,购物车的例子。

在这里插入图片描述

创建 feature 文件

创建一个 shopping_cart.feature, 为每个 Scenario 加了一些 Tags

# shopping_cart.feature

Feature: Shopping Cart and Order Process

  @cart @smoke
  Scenario: Guest user adds items to the cart
    Given the user is on the home page
    When the user adds an item to the cart
    Then the user should see the item in the cart

  @cart @regression
  Scenario: Registered user removes items from the cart
    Given the user is logged in
    And the user has items in the cart
    When the user removes an item from the cart
    Then the user should see the updated cart

  @order @smoke
  Scenario: Guest user places an order
    Given the user is on the home page
    When the user adds an item to the cart
    And the user proceeds to checkout
    And the user completes the order
    Then the user should receive an order confirmation

  @order @regression
  Scenario: Registered user tracks an order
    Given the user is logged in
    And the user has placed an order
    When the user checks the order status
    Then the user should see the current order status

创建 step 实现

创建 shopping_cart_steps.py 文件

# shopping_cart_steps.py
from behave import given, when, then

class ShoppingCart:
    def __init__(self):
        self.cart_items = []
        self.is_logged_in = False
        self.checkout_completed = False
        self.order_placed = False

    def add_item_to_cart(self, item):
        self.cart_items.append(item)

    def remove_item_from_cart(self, item):
        self.cart_items.remove(item)

    def proceed_to_checkout(self):
        self.checkout_completed = True

    def complete_order(self):
        self.order_placed = True

    def check_order_status(self):
        return "Shipped" if self.order_placed else "Pending"

@given('the user is on the home page')
def step_given_user_on_home_page(context):
    context.shopping_cart = ShoppingCart()

@given('the user is logged in')
def step_given_user_logged_in(context):
    context.shopping_cart = ShoppingCart()
    context.shopping_cart.is_logged_in = True

@given('the user has items in the cart')
def step_given_user_has_items_in_cart(context):
    context.shopping_cart.add_item_to_cart("Sample Item")

@when('the user adds an item to the cart')
def step_when_user_adds_item(context):
    context.shopping_cart.add_item_to_cart("Sample Item")

@when('the user removes an item from the cart')
def step_when_user_removes_item(context):
    context.shopping_cart.remove_item_from_cart("Sample Item")

@when('the user proceeds to checkout')
def step_when_user_proceeds_to_checkout(context):
    context.shopping_cart.proceed_to_checkout()

@when('the user completes the order')
@given('the user has placed an order')
def step_when_user_completes_order(context):
    context.shopping_cart.complete_order()

@when('the user checks the order status')
def step_when_user_checks_order_status(context):
    context.order_status = context.shopping_cart.check_order_status()

@then('the user should see the item in the cart')
def step_then_user_sees_item_in_cart(context):
    assert "Sample Item" in context.shopping_cart.cart_items, "Item not found in the cart"

@then('the user should see the updated cart')
def step_then_user_sees_updated_cart(context):
    assert not context.shopping_cart.cart_items, "Cart is not empty as expected"

@then('the user should receive an order confirmation')
def step_then_user_receives_order_confirmation(context):
    assert context.shopping_cart.order_placed, "Order confirmation not received"

@then('the user should see the current order status')
def step_then_user_sees_current_order_status(context):
    assert context.order_status == "Shipped", f"Unexpected order status: {context.order_status}"

Tag 过滤执行

执行单个标签 --tags=tagname

只要运行标记 @smoke 的所有 cases,只需在 Behave 执行命令加上 --tags=smoke
执行命令:behave Features/tag_example --tags=smoke -f behave_html_formatter:HTMLFormatter -o report.html

PS C:\Automation\Test\bdd> behave Features/tag_example --tags=smoke -f behave_html_formatter:HTMLFormatter -o report.html
1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 2 skipped
8 steps passed, 0 failed, 8 skipped, 0 undefined
Took 0m0.007s

查看 html report,就只有两个标记为 @smoke 的 Scenarios 执行了,剩下的都被 skipped 了。

在这里插入图片描述

执行多个标签 OR 关系 --tags=tag1,tag2

可以通过用逗号 , (逻辑’ OR ')分隔标签名来同时执行 2 个标签。因此,下面我们运行那些标记为 @smoke 或 @cart 的测试用例,只需用逗号分开 tags, --tags=smoke,cart

执行命令:behave Features/tag_example --tags=smoke,cart -f behave_html_formatter:HTMLFormatter -o report.html

PS C:\Automation\Test\bdd> behave Features/tag_example --tags=smoke,cart -f behave_html_formatter:HTMLFormatter -o report.html
1 feature passed, 0 failed, 0 skipped
3 scenarios passed, 0 failed, 1 skipped
12 steps passed, 0 failed, 4 skipped, 0 undefined
Took 0m0.000s

查看 html report,标记为 @smoke 或 @cart 的 Scenarios 执行了,剩下的都被 skipped 了。

在这里插入图片描述
而官方推荐的 --tags=“@tag1 or @tag2” 实践下来无效,不知是不是版本问题。

PS C:\Automation\Test\bdd> behave Features/tag_example --tags="@smoke or @cart" -f behave_html_formatter:HTMLFormatter -o report.html
0 features passed, 0 failed, 1 skipped
0 scenarios passed, 0 failed, 4 skipped
0 steps passed, 0 failed, 16 skipped, 0 undefined
Took 0m0.000s

多个标签 AND 关系 --tags=tag1 --tags=tag2

只想运行标记为 @smoke 并且标记为 @cart 的测试用例,–tags=smoke --tags=cart
执行命令:behave Features/tag_example --tags=smoke --tags=cart -f behave_html_formatter:HTMLFormatter -o report.html

PS C:\Automation\Test\bdd> behave Features/tag_example --tags=smoke --tags=cart -f behave_html_formatter:HTMLFormatter -o report.html
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 3 skipped
3 steps passed, 0 failed, 13 skipped, 0 undefined
Took 0m0.000s

查看 html report,标记为 @smoke 且标记为 @cart 的 Scenarios 执行了,剩下的都被 skipped 了。

在这里插入图片描述

而官方推荐的 --tags=“@tag1 and @tag2” 实践下来无效。

PS C:\Automation\Test\bdd> behave Features/tag_example --tags="@smoke and @cart" -f behave_html_formatter:HTMLFormatter -o report.html
0 features passed, 0 failed, 1 skipped
0 scenarios passed, 0 failed, 4 skipped
0 steps passed, 0 failed, 16 skipped, 0 undefined
Took 0m0.000s

单标签非关系 --tags = ~tagname

只想运行标记非 @smoke 的 cases,我们需要在 tag 前面加是破浪号 ~, --tags=~smoke
执行命令:behave Features/tag_example --tags=~smoke -f behave_html_formatter:HTMLFormatter -o report.html

PS C:\Automation\Test\bdd> behave Features/tag_example --tags=~smoke -f behave_html_formatter:HTMLFormatter -o report.html
1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 2 skipped
8 steps passed, 0 failed, 8 skipped, 0 undefined
Took 0m0.000s

查看 html report,非 @smoke 标记的 Scenarios 执行了,剩下的都被 skipped 了。

在这里插入图片描述
而官方推荐的 --tags=“not @tag” 我实践无效。

PS C:\Automation\Test\bdd> behave Features/tag_example --tags="not @smoke" -f behave_html_formatter:HTMLFormatter -o report.html
0 features passed, 0 failed, 1 skipped
0 scenarios passed, 0 failed, 4 skipped
0 steps passed, 0 failed, 16 skipped, 0 undefined
Took 0m0.000s

文章来源:https://blog.csdn.net/wumingxiaoyao/article/details/135168124
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。