Reputation: 972
I am using behave fixtures to create a counter during test run. I initialize the counter in before_all
hook, and later increment it in before_scenario
every time a scenario is running.
I thought before_all
runs once during the entire test, and if a variable is added to the context it is available for later.
Here I am initializing context.i = 0
in before_all
, but in before_scenario
every time a scenario is run, the value of context.i
is again set to 0.
environment.py
def before_all(context):
context.i = 0
def before_scenario(context, scenario):
context.i = context.i + 1
I want to increment i
with every run. But it is always set to 1
.
Upvotes: 1
Views: 2665
Reputation: 972
For those having a similar purpose. You can use a dict attribute and store the variables as keys. This way, every time a new stack is added to the frame the context dict is preserved.
def before_all(context):
context.mydata = {}
context.i = 0 // will be overridden
context.mydata['i'] = 1 // will be preserved
def before_scenario(context, scenario):
context.mydata['i'] += 1
context.i += 1
return
Upvotes: 2
Reputation: 180
Context user variables, defined in before_scenario accessible only in scenario lifycycle. When runner start next scenario, context.i
defined in last scenario is no longer exist, so it uses variable, defined in before_all hook.
I guess you can't change context user variable defined in before_all hook.
Upvotes: 0