簡體   English   中英

pytest-bdd為不同的步驟重用相同的方法

[英]pytest-bdd reuse same method for different steps

我想對同一步的@given,@ when和@then使用相同的方法。 例如

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

如果我在執行步驟中執行此操作:

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

看來,pytest_bdd無法處理這個問題。 我收到錯誤:

夾具'登錄按鈕按下'未找到有沒有辦法我可以將步驟別名?

在pytest-BDD中,它目前不支持在一個函數定義上使用兩個不同的步驟類型/定義的能力。 怎么會有“解決方法”

選項1:關閉Gherkin嚴格模式

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

優點:不重復功能體代碼,保持給定 - >時 - >然后模式。 仍然可維護(更改代碼1位)

缺點:仍然需要為給定的時間創建新的函數定義,然后是版本......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM