在场景大纲之前运行一次特定步骤 - Python Behave

Nig*_*ach 6 python bdd scenarios python-behave

正如标题所示,我希望在场景大纲之前运行某些配置/环境设置步骤.我知道有Background这样做的场景,但Behave将场景大纲分成多个场景,从而为场景大纲中的每个输入运行背景.

这不是我想要的.由于某些原因,我无法提供我正在使用的代码,但是我将编写一个示例功能文件.

Background: Power up module and connect
Given the module is powered up
And I have a valid USB connection

Scenario Outline: Example
    When I read the arduino
    Then I get some <'output'>

Example: Outputs
| 'output' |
| Hi       |
| No       |
| Yes      |
Run Code Online (Sandbox Code Playgroud)

什么会发生在这种情况下是会舞动动力循环和检查每个输出USB接口Hi,No,Yes导致三个电源周期和三个连接检查

我想要的是使用一次电源循环并检查连接一次,然后运行所有三个测试.

我该怎么做呢?

Tom*_*Tom 7

您最好的选择可能是使用before_feature环境挂钩和 a) 功能上的标签和/或 b) 直接使用功能名称。

例如:

一些.feature

@expensive_setup
Feature: some name
  description
  further description

  Background: some requirement of this test
    Given some setup condition that runs before each scenario
      And some other setup action

  Scenario: some scenario
      Given some condition
       When some action is taken
       Then some result is expected.

  Scenario: some other scenario
      Given some other condition
       When some action is taken
       Then some other result is expected.
Run Code Online (Sandbox Code Playgroud)

步骤/环境.py

def before_feature(context, feature):
    if 'expensive_setup' in feature.tags:
        context.execute_steps('''
            Given some setup condition that only runs once per feature
              And some other run once setup action
        ''')
Run Code Online (Sandbox Code Playgroud)

替代步骤/environment.py

def before_feature(context, feature):
    if feature.name == 'some name':
        context.execute_steps('''
            Given some setup condition that only runs once per feature
              And some other run once setup action
        ''')
Run Code Online (Sandbox Code Playgroud)


bot*_*que 0

我面临着完全相同的问题。有一个昂贵的 Background,应该只执行一次Feature。解决这个问题实际上需要的是在 s 之间存储状态的能力Scenario

我对这个问题的解决方案是使用behave.runner.Context#_root在整个运行过程中保留的内容。我知道访问私人成员不是一个好的做法 - 我会很高兴学习更干净的方式。

# XXX.feature file
Background: Expensive setup
  Given we have performed our expensive setup

# steps/XXX.py file
@given("we have performed our expensive setup")
def step_impl(context: Context):    
    if not context._root.get('initialized', False):
        # expensive_operaion.do()
        context._root['initialized'] = True
Run Code Online (Sandbox Code Playgroud)