I'm using Python & Behave BDD for automation.
As I know, background runs before each scenario, but I need background to run before scenarios only with @need_background
tag. How can I achieve this?
I have tried to get current scenario tag and 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.
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