Is there an easy way to define a step as being both a Given and a When

Question:

I am building a Py Behave test framework and have a number of scenarios where what was previously a When step becomes a Given

E.G in one scenario

Given a user has is on the logon page 
When they login with credentials <user>
Then the user logs in 

But in other scenarios

Given a user is on the logon page
And they login with credentials <user>

In my steps this would appear as

 @given('they login with credentials {user}')
 def step_impl(context):
    Do login code

 @when('they login with credentials {user}')
 def step_impl(context):
    Do login code

Is there a way to save having to write all these steps out twice, but be able to define Whens as Givens?

Asked By: Richard C

||

Answers:

You can use @step decorator provided in behave

Scenario one

Given a user has is on the logon page 
When they login with credentials <user>
Then the user logs in

Scenario two

Given a user is on the logon page
And they login with credentials <user>

Solution:

@step('they login with credentials {user}')
 def step_impl(context):
    Do login code

reference: https://github.com/behave/behave/issues/550

Answered By: Vikas Mulaje

@given(‘they login with credentials {user}’)
@when(‘they login with credentials {user}’)
def step_impl(context):
Do login code

Answered By: 0102_Dheeraj Kumar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.