Pytest-bdd reuse the same method for different steps

I want to use the same method for @given, @when and @then of the same step. eg

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

If I do this while performing the step:

@when('login button pressed')
@given('login button pressed')
def loginButtonPressed():
    print 'button pressed'

It seems pytest_bdd cannot handle this. I get an error message:

Connect the login button “not pressed” Is there a way that I can possibly complete these steps?

+4
source share
1 answer

In pytest-BDD, it currently does not support the ability to use two different types / definitions of steps in a single function definition. Whatever the "work around"

Option 1: Turn off strict hardness mode

@pytest.fixture
def pytestbdd_strict_gherkin():
    return False

:

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

Scenario: Use given anywhere         
         Given username and password entered
         when login button pressed
         then I am logged in
         when login button pressed

: // ...

: ... .

.

2.

@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() 

pros: , - > - > . ( 1 )

: , , ...

+1

Source: https://habr.com/ru/post/1652764/


All Articles