首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何跳过Behave后台的步骤?

如何跳过Behave后台的步骤?
EN

Stack Overflow用户
提问于 2017-01-13 23:03:45
回答 1查看 1.7K关注 0票数 0

我使用Python & Behave实现自动化。

据我所知,后台在每个场景之前运行,但我需要后台才能在场景之前运行,只使用@need_background标签。我如何才能做到这一点?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-14 05:26:24

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

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

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

代码语言:javascript
复制
def before_scenario(context, scenario):
if 'need_background ' in scenario.tags:
    context.if_background = True
else:
    context.if_background = False    

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

代码语言:javascript
复制
@given('all background steps are done')
def step_impl(context):
if context.if_background:
    context.context.execute_steps('''
        steps in background
    ''')
else:
    pass

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

代码语言:javascript
复制
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

我想它也许能满足你的要求。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41637534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档