pytest-bdd reuse same method for different steps
<p>I want to use same method for @given, @when and @then of same step.
e.g. </p>

<pre><code>Scenario: Launch an application         
    Given username and password entered
    And login button pressed
    Then the app launches

Scenario: Launch application again          
    Given user logged out
    And username and password entered
    When login button pressed
    Then the app launches
</code></pre>

<p>If I do this in the implementation of step:</p>

<pre><code>@when('login button pressed')
@given('login button pressed')
def loginButtonPressed():
    print 'button pressed'
</code></pre>

<p>It seems, pytest_bdd cannot handle this. I get the error: </p>

<blockquote>
  <p>fixture 'login button pressed' not found 
  Is there a way I can maybe alias the steps?</p>
</blockquote>

<p>In pytest-BDD it does not currently support the ability to use two different step types/definitions on one function definition. How ever there are "work arounds"</p>

<p><strong>Option 1: Turn off Gherkin strict mode</strong> </p>

<pre><code>@pytest.fixture
def pytestbdd_strict_gherkin():
    return False
</code></pre>

<p>This will allow you to use steps in any order:</p>

<pre><code>@when('login button pressed')
def loginButtonPressed():
    print 'button pressed'
</code></pre>

<p>In gherkin</p>

<pre><code>Scenario: Use given anywhere         
         Given username and password entered
         when login button pressed
         then I am logged in
         when login button pressed
</code></pre>

<p><em>pros:</em> don't have to create given/when/then versions...</p>

<p><em>cons:</em> can make less readable... goes against true step procedure. </p>

<p>see for more info <a href="http://pytest-bdd.readthedocs.io/en/latest/#relax-strict-gherkin-language-validation" rel="nofollow">Relax strict gherkin</a></p>

<p><strong>Option 2: Call previously defined function in new step definition</strong></p>

<pre><code>@given('login button pressed')
    def loginButtonPressed():
        # do stuff...
        # do more stuff...
        print 'button pressed'

@when('login button pressed')
    def when_loginButtonPressed():
        loginButtonPressed() 

@then('login button pressed')
    def then_loginButtonPressed():
        loginButtonPressed() 
</code></pre>

<p><em>pros:</em> Doesn't duplicate function body code, keep given->when->then pattern. Still maintainable (change code 1 place) </p>

<p><em>cons:</em> Still have to create new function definitions for given, when, then versions... </p>

python bdd py.test