pythonpython-behave

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


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?


Solution

  • 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