我使用Python & Behave实现自动化。
据我所知,后台在每个场景之前运行,但我需要后台才能在场景之前运行,只使用@need_background标签。我如何才能做到这一点?
我已经尝试获取当前场景的tag和if tag != need_background then skip background's steps。但据我所知,behave没有跳过后台步骤的方法。
发布于 2017-01-14 05:26:24
既然这些场景不共享相同的背景,为什么不将特殊的场景移动到其他功能文件中,或者干脆不使用背景。
但是如果你仍然想使用background部分,我建议:
首先,在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我想它也许能满足你的要求。
https://stackoverflow.com/questions/41637534
复制相似问题