9

正如标题所示,我希望在场景大纲之前运行一些特定的配置/环境设置步骤。我知道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      |

在这种情况下会发生的情况是 Behave 将重启电源并检查每个输出的 USB 连接,Hi导致三个电源重启和三个连接检查NoYes

我想要的是 Behave 重启一次并检查一次连接,然后运行所有三个测试。

我该怎么做呢?

4

3 回答 3

6

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

例如:

一些特征

@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.

步骤/环境.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
        ''')

备用步骤/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
        ''')
于 2016-12-02T02:18:37.970 回答
0

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

我对这个问题的解决方案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
于 2016-01-18T07:56:28.147 回答
0

你可以这样做 :

  1. 在 Features 文件夹中创建 environment.py

  2. 内部 environment.py :从行为导入夹具

  3. 编写代码:

从行为导入夹具

def before_feature(context, feature): print("在每个功能之前运行")

def after_feature(context, feature): print("在每个特征之后运行")

def before_scenario(context,scenario): print("在每个场景之前运行")

def after_scenario(context,scenario): print("Run After Each Scenario")

#Now run your Tc : 您的 O/P 将在每个功能之前运行 每个场景之前运行 每个场景之后每个功能之后运行

于 2020-07-20T19:38:23.427 回答