2013-10-15 64 views
9

我正在編寫一個驗證測試,在希望測試基於初始操作的Web應用的UI中的多個更改的小黃瓜中。下面是一個示例:可以在單個小黃瓜場景中有多組Given/When/Then

 Scenario: Cancel editing a new text asset 
      Given the user "[email protected]" is logged in 
      When the user navigates to "/build/" 
      And the user clicks the "Sandbox" link 
      And the user inputs "Test story for canceling editing of a new text asset" for the "title" field 
      And the user inputs "Test User" for the "byline" field 
      And the user inputs "My summary, so exciting!" for the "summary" textarea 
      And the user clicks on "Untitled Section" in the section list 
      And the user clicks the "Text" icon in the "center" container 
      And the user inputs the following text in the rich text editor: 
        """ 
        Test text for asset. This is cool. 
        """ 
      And the user clicks the "cancel" button 
      Then the following text is not present: 
        """ 
        Test text for asset. This is cool. 
        """ 
      And the "Image" icon is present 
      And the "Text" icon is present 
      When the user refreshes the browser 
      And the user clicks on "Untitled Section" in the section list 
      Then the following text is not present: 
        """ 
        Test text for asset. This is cool. 
        """ 
      When the user opens the asset drawer 
      Then the following text is not present: 
        """ 
        Test text for asset. This is cool. 
        """ 

請注意,有多組When/Then步驟來測試初始操作的響應。雖然大多數步驟的實現忽略前綴關鍵字,並且我非常確定我可以運行此測試,但有沒有更好的方法來測試不同的結果?使用相同的設置但不同的「Then」語句編寫多個場景是否更好?

+0

嗯。不回答是否可能的問題。現在到更多谷歌搜索... – djangofan

+0

可能的重複[是否可以寫一個「給予當然後當然後」測試在小黃瓜?](https://stackoverflow.com/questions/12060011/is-it-acceptable-當我在小黃瓜上試驗的時候寫下一個給定的答案) –

+0

我不同意接受的答案;我添加了[我的回答](https://stackoverflow.com/a/45245799/634576)這是一個重複的問題。 –

回答

19

請記住,您應該一次只測試一個行爲/功能。 經驗法則是,你應該使用只有當步驟

Given some state before action 
    And some other state before action 
    ... 
When only one action 
Then assert correct output 
    And assert correct output 
    ... 

你看 - 如果,沒有任何ANDS下,當只有一行。如果您使用許多When步驟,則可以創建測試腳本,而不是規範。您的測試很難理解,並且您會注意到當底層實現發生更改時,您會添加越來越多的步驟。

您還需要保持潛在的邏輯隱藏,因爲您不想每次更改無關的內容時都更改它。例如:

而用戶輸入「我的總結,太令人興奮了!」 「摘要」 textarea

如果將摘要字段從textarea更改爲輸入類型,該怎麼辦?你必須改變場景(維護噩夢)或讓你的場景撒謊(比沒有場景更糟糕)。你應該寫:

When the user describes it as "so exciting!" 

但仍然,整個場景的結構是不好的。問自己這個問題:我想檢查什麼?如果我是那個想了解該功能的業務邏輯的人,我想看到的東西,如:

Scenario: Cancel editing a new text asset 
    Given I edit the Sandbox details with some data 
    When I cancel editing 
    Then Sandox details should be empty 

這就是它!

如何實現它?移動所有不相關的邏輯更深入,使用PageObject pattern等。並閱讀約Specification By Example

+0

感謝您的有益迴應。我聽到你在說什麼,使規範反映了一個功能的核心業務邏輯,從而使他們更加面向未來。 那麼使用功能/場景來測試特定功能的特定UI實現是否不合適?如果是這樣,什麼會更合適? 我明白在我的示例中,功能和特定UI組件之間的邊界存在一些不明確之處。 –

+0

1.對於UI測試,只有在使用小黃瓜時(因爲使用過的工具,框架等),這是最快的方法。 2。如果您使用小黃瓜與其他人交流,則專注於用戶目標,而不是UI,並使用PageObjects來包裝基礎邏輯。對於前者使用最快的工具 - 大多數時間商務人士不會在textareas和輸入字段之間產生差異。例如,我只使用PageObjects + Javascript單元測試來測試UI邏輯,因爲在我們的例子中這是最快的方法。 –

+1

小黃瓜是爲了涵蓋您的業務規則,而不是您的代碼。 BDD關於應用程序設計,而不是應用程序測試。如果你有需要測試的功能元素,那麼使用一個功能測試框架或一套綜合的單元測試。 –