【问题标题】:How to skip steps in background of Behave BDD?如何在 Behave BDD 后台跳过步骤?
【发布时间】:2017-01-13 15:03:45
【问题描述】:

我正在使用 Python & Behave BDD 进行自动化。

据我所知,后台在每个场景之前运行,但我需要后台在带有@need_background 标签的场景之前运行。我怎样才能做到这一点?

我试图获取当前场景标签和if tag != need_background then skip background's steps。但据我所知,behaviour 没有跳过后台步骤的方法。

【问题讨论】:

    标签: python bdd python-behave


    【解决方案1】:

    既然场景不共享相同的背景,为什么不将特殊的移动到其他功能文件或不使用背景。

    但如果你仍然想使用背景部分,我建议:

    首先,在你的 environment.py 中添加一个钩子

    def before_scenario(context, scenario):
    if 'need_background ' in scenario.tags:
        context.if_background = True
    else:
        context.if_background = False    
    

    然后将你在后台的所有步骤合并为一个步骤

    @given('all background steps are done')
    def step_impl(context):
    if context.if_background:
        context.context.execute_steps('''
            steps in background
        ''')
    else:
        pass
    

    现在,如果您的功能文件是:

    Feature: Background with condition
    Background:
    Given all background steps are done
    
    Scenario: run without background
    # steps of the scenario you don't need background
    
    @need_background 
    Scenario: run with background
    # steps of the scenario you need background
    

    我认为它可能满足您的要求

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 2019-11-19
      • 2020-10-14
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多