如何在 Behave BDD 中处理这种 BACKGROUND 情况

How to handle this BACKGROUND case in Behave BDD

我刚刚开始使用 Python & Behave BDD 进行自动化。

我的一个测试套件包括以下部分:

0) After login on the web page
1) to create a new profile 
2) to view the created profile in the profile list. 
3) to update the created profile 
4) to have the profile deleted. 

现在我想把所有这些都放在一个功能文件中,比如:

Feature: Profile behaviour testing

Background: User login
...

Scenario: creating new profile
...

Scenario: viewing created profile
...

Scenario: updating generated profile
...

Scenario: deleting created profile 
....

但问题是后台部分适用于每个单独的场景,对于每个场景,它从登录会话开始。

有没有办法让它只发生一次?

谢谢,

Background 关键字实际上是比任何东西都多的语法糖,将 Background 中的步骤应用于功能中的每个场景。如果你只想记录一次用户,你可以尝试在上下文对象中管理它。

鉴于此行为结构:

├── behave.ini
└── features
    ├── environment.py
    ├── login-once.feature
    └── steps
        └── steps.py

behave.ini:

[behave]
stdout_capture = false
stderr_capture = false
log_capture = false

登录-once.feature:

Feature: User Login only actually happens once

    Background: User Login
        Given the user is logged in

    Scenario: User clicks Home
        When the user clicks the Home button
        Then the Home page is shown

    Scenario: User clicks About
        When the user clicks the About button
        Then the About page is shown

environment.py:

# Behave has an interesting page/heap structure behind the scenes
# that obfuscates what is and isn't in the context object.
# This class is instantiated in the before_all function and used
# to assign values, so that they persist across scenario tests

class Holder(object):
    pass


def before_all(context):
    context.holder = Holder()


def before_feature(context, feature):
    context.holder.logged_in = False

steps.py

from behave import given, then, when


@given('the user is logged in')
def user_logged_in(context):
    if context.holder.logged_in is True:
        print("\t\tThe user is already logged in, will not log in again")
    else:
        print("\t\tLogging user in for the first time")
        context.holder.logged_in = True

@when('the user clicks the {button} button')
def user_clicks_button(context, button):
    print(f"\t\tThe user clicked the {button} button")

@then('the {page} page is shown')
def page_is_shown(context, page):
    print(f"\t\tThe {page} page is being shown")

当我现在 运行 行为时,我得到以下输出:

$ behave -f plain
Feature: User Login only actually happens once
  Background: User Login

  Scenario: User clicks Home
        Logging user in for the first time
    Given the user is logged in ... passed in 0.000s
        The user clicked the Home button
    When the user clicks the Home button ... passed in 0.000s
        The Home page is being shown
    Then the Home page is shown ... passed in 0.000s

  Scenario: User clicks About
        The user is already logged in, will not log in again
    Given the user is logged in ... passed in 0.000s
        The user clicked the About button
    When the user clicks the About button ... passed in 0.000s
        The About page is being shown
    Then the About page is shown ... passed in 0.000s

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s