简体   繁体   English

如何在Behave BDD后台跳过步骤?

[英]How to skip steps in background of Behave BDD?

I'm using Python & Behave BDD for automation. 我正在使用Python&Behave BDD进行自动化。

As I know, background runs before each scenario, but I need background to run before scenarios only with @need_background tag. 据我所知,背景运行在每个方案之前,但是我需要背景运行在仅带有@need_background标记的方案之前。 How can I achieve this? 我该如何实现?

I have tried to get current scenario tag and if tag != need_background then skip background's steps . 我试图获取当前场景标签, if tag != need_background then skip background's steps But behave doesn't have a method for skipping background steps as far as I am aware. 但是据我所知,behavior没有跳过背景步骤的方法。

Since the scenarios are not sharing the same background, why not moving the special one to other feature files or just not using background. 由于方案没有共享相同的背景,为什么不将特殊文件移动到其他功能文件或不使用背景。

But if you still want to use background section, I would recommend: 但是,如果您仍然想使用背景部分,我建议:

Firstly, Add a hook to your environment.py 首先,为您的环境添加一个钩子。

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

Then combine all your steps in background as one step 然后将您所有的步骤在后台合并为一个步骤

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

Now, if your feature file is: 现在,如果您的功能文件是:

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

I think it might meet your requirements 我认为它可能符合您的要求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM