2011-03-30 128 views
9

我們是新的bdd /黃瓜和討論在隊伍中如何編寫正確的功能/場景。最好的做法,寫真正的bdd黃瓜功能/場景

我們想出了兩個下列辦法,幾乎應該說明/解決同樣的要求:

Feature: Give access to dossiers to other registered users 
    As a logged in user 
    In order to show my dossier to other users 
    I want to give other users (limited) access to my dossiers 

    Background: 
    Given I am logged in as "Oliver" 
    And another user "Pascal" exists 
    And another user "Tobias" exists 

    Scenario: I can give access to my own dossier 
    When I grant access to "Pascal" with permisson "readonly" 
    Then I should see "Access granted." 
    And user "Pascal" should have permission "readonly" on dossier "Oliver" 

    Scenario: I can give access to a created dossier 
    Given I created a new dossier "Max Müller" 
    When I grant access on dossier "Max Müller" to "Pascal" with permisson "readonly" 
    Then I should see "Access granted." 
    And user "Pascal" should have permission "readonly" on dossier "Max Müller" 

    Scenario: I can give access to a managed dossier 
    Given I manage the dossier from "Tobias" 
    When I grant access on dossier "Tobias" to "Pascal" with permisson "readonly" 
    Then I should see "Access granted." 
    And user "Pascal" should have permission "readonly" on dossier "Tobias" 

    Scenario: I cannot give access to a writable dossier 
    Given I have write access to the dossier from "Tobias" 
    When I follow "Grant access" 
    Then I should see "You are not allowed to grant access on this dossier." 

    Scenario: I cannot give access to a readonly dossier 
    Given I have readonly access to the dossier from "Tobias" 
    When I follow "Grant access" 
    Then I should see "You are not allowed to grant access on this dossier." 

    Scenario: I can give access to a dossier with an expiration date 
    Given I created a new dossier "Max Müller" 
    When I grant access on dossier "Max Müller" to "Pascal" with permisson "readonly" until "2020-01-01" 
    Then I should see "Access granted till 2020-01-01." 
    And user "Pascal" should have permission "readonly" on dossier "Max Müller" until "2020-01-01" 

    Scenario: I cannot transfer a created dossier to a new owner who is already registered 
    Given I created a new dossier "Max Müller" 
    When I transfer dossier "Max Müller" to "Pascal" 
    Then I should see "Pascal already has a dossier, transfer not possible." 

第二個:

Feature: Grant access on dossiers to registered users 
    As a logged in user 
    In order to allow others to view/ manage dossiers I have access to 
    I want to give access of those to other users 

    Background: 
    Given I am logged in as "[email protected]" 
    And I am working with my own dossier 

    Scenario: Invalid data entered 
    When I visit the grant dossier access page 
    And I press "Grant access" 
    Then I should see a validation error on "eMail-Address" 

    Scenario: Valid data entered 
    Given a user "[email protected]" exists 
    When I visit the grant dossier access page 
    And I fill in "eMail-Address" with "[email protected]" 
    And I select "readonly" from "Permissions" 
    And I press "Grant access" 
    Then I should see "Access granted." 
    And I should be on the dossiers page 

    Scenario: Valid data entered with expiry date 
    Given a user "[email protected]" exists 
    When I visit the grant dossier access page 
    And I fill in "eMail-Address" with "[email protected]" 
    And I select "readonly" from "Permissions" 
    And I fill in "Valid until" with "2010-03-01" 
    And I press "Grant access" 
    Then I should see "Access granted till 2010-03-01." 
    And I should be on the dossiers page 

    Scenario: Display calendar on click on "Valid until" 
    When I click on the field "Valid until" 
    Then a calendar popup should be displayed 
    When I click on "1943-01-02" 
    Then the field "Valid until" should be have "1943-01-02" 
    And the calendar popup should be hidden 

    Scenario: Only allow to grant access to categories I have access to myself 
    Given I have limited access to the working dossier 
    When I visit the grant dossier access page 
    Then I should not see categories I have no access to 

    Scenario: Dossier with permission "manager" can only grant readonly, readwrite 
    Given I have the permission "manager" on my working dossier 
    When I visit the grant dossier access page 
    Then I should only see the permissions "readonly, readwrite" 

    Scenario: Dossier with permission "readwrite" is not allowed to grant any permissions 
    Given I have the permission "readwrite" on my working dossier 
    When I visit the grant dossier access page 
    Then I should the see the error "You cannot grant access on this dossier!" 
    And I should be on the dossiers page 

一個你會喜歡哪一種?爲什麼?

+2

有點讓人從死裏復活,但是有沒有一個理由,你還沒有選擇一個答案呢?無論如何,你最終會做什麼?它是如何發揮作用的? – KobeJohn 2012-01-16 16:28:05

回答

6

我會選擇一種更易於客戶或開發者以外的人閱讀的解決方案。使用像Cucumber這樣的工具的一大優點是它看起來像一個故事。您的目標應該是使每個測試都足夠簡單,以便客戶可以閱讀您的驗收測試並瞭解哪些功能已經實施。這對您有好處,因爲您的客戶可以輕鬆地知道某些功能具有驗收測試。

話雖如此,我認爲第一個解決方案比第二個解決方案更能代表可讀的風格。

「我可以給訪問的檔案與 到期日期」

是很容易,

「與到期日期輸入有效的數據」 讀

在我看來。

你不一定需要在每個場景前面添加「我可以」或「我不能」,但是可以放在一個完整的想法旁邊。

更新

在你的評論被問及驗證錯誤,你應該如何處理與選項1.我覺得你有幾個選擇這裏:

  1. 黃瓜全面驗證測試
  2. Cucumber中的部分驗證測試(向用戶顯示錯誤),RSpec中的部分測試(引發錯誤) - 或其他單元測試框架。

選項1

優點:

您不必使用其他框架來測試您的驗證打擾。你的大部分測試將被包含在黃瓜中。您的客戶可以在一個地方看到所有的測試。

缺點:

您黃瓜的測試將具有粒度的不同級別包括在內。您的客戶可能更關心高級功能,而不是看到所有細節的細節通過。如果我是一個有興趣使用Cucumber測試作爲已經實現和測試過哪些功能的基礎的客戶,我寧願選擇比所有測試案例更小,更易讀的測試案例列表。我最多可能希望看到錯誤消息呈現給用戶 - 您可以在一個場景大綱中執行此操作。

選項2

優點:

你分開你的測試問題納入粒度的適當水平。正如我在選項1中提到的那樣,您的客戶很可能對較低級別的細節感興趣。您可以在RSpec單元測試中測試驗證是否通過,並且使用Cucumber快速測試用戶是否看到錯誤消息(如果有無效記錄)(再次使用場景輪廓,或者只是測試以查看是否只有一條驗證消息它到前端)。主要的觀點是更多功能測試的Cucumber不應該測試關於Models的每一件小事情 - 讓RSpec或其他單元測試框架來處理它。

缺點:

你必須使用另一個框架行使更細粒度的預期。使用另一個框架意味着需要花費更多的時間來學習它等等。另外,很難知道如何平衡何時使用一個框架,即「我應該在這裏使用Cucumber還是RSpec?」。

從我讀過的,選項2是目前最流行的。團隊使用適合每個粒度級別的框架,他們試圖遠離「全進」方法。

+0

你會如何處理前。驗證錯誤?你如何/在哪裏描述當前例會發生什麼?輸入了無效的電子郵件地址?這種行爲不重要嗎? – gucki 2011-04-05 12:06:30

+0

@gucki - 我更新了我的回覆。我希望這項努力至少能讓我獲得一個贊成票:P – McStretch 2011-04-05 12:31:02

4

我更喜歡第一種方案,因爲它在一個更好的辦法的功能是如何工作的

此外,你必須測試的重構與方案綱要在那裏做,檢查本博客文章介紹,這是偉大的工作黃瓜http://eggsonbread.com/2010/09/06/my-cucumber-best-practices-and-tips/

在第一種方法,你必須做你自己的步驟,但你會使用更多的時間相同的情況下,減少代碼重複,當你有:

我填寫「電子郵件地址」與「[email protected]」 而我填寫「電子郵件地址」w ith「[email protected]

在不同的情況下。我希望在這裏指出我的觀點。

此外,我強烈建議您the rspec booktalk called outside-in development with cucumber

問候

+0

我完全同意,功能/場景應該是乾的。所以我會先從一個命令式的風格開始,然後重構,給我一個更具說明性的風格(不會丟失任何UI測試或文檔)。正如我在下面的另一條評論中寫的,我不喜歡示例1,因爲我認爲它看起來更像應該放在功能測試中的內容(...然後,用戶「Pascal」應該對「MaxMüller」檔案擁有「只讀」權限) 。它也完全忽略了用戶交互。 – gucki 2011-04-05 12:03:54

2

這種BDD的整點是對於非技術決策者提供可讀的,可以理解的規格/校長組織內(以減少決策者和執行者之間的溝通困境)。

爲此,您的第一個示例中的樣式對於第二個示例而言是無可挑剔的。簡單。

+0

我對第一個例子的擔憂是,您實際上並沒有定義用戶必須處理的行爲。它看起來更像是一個功能測試。 – gucki 2011-04-05 11:55:20

+0

這真的取決於你正在處理的是什麼樣的觀衆(校長)。如果他們熟悉你的應用的UI和Ux,那麼第二個例子更合適。但是,對於那些不熟悉UI/Ux的人來說,第一個例子會更好。如果我不得不在兩者之間進行選擇(不瞭解我的觀衆),那麼在我看來,這種BDD對於後一種情況更爲重要(校長與UI不密切),這就是爲什麼我建議使用第一個例子 - - 但如果你知道你是觀衆,盡一切努力,你應該迎合他們! – JoshuaDavid 2011-04-05 15:24:47

1

我更喜歡兩者的第二種方法。這是我對方法1的擔憂。

當我授予檔案存取「最大 穆勒」到「帕斯卡」與在未經許可的 「只讀」

可以不通過用戶界面,這是一個危險的去實現。如果對用戶授予訪問權限的方式有一個完整的期望,那麼這些期望就不會被測試。黃瓜的光芒正在從UI向下測試(通過UI進行測試)。

我對

而且用戶「帕斯卡」同樣值得關注應該有 許可「只讀」的檔案「最大 穆勒」

有可能要實施的每個步驟在步驟定義中通過調用通過UI執行的步驟,但除非這是真的,否則我仍然會有這種擔憂。

總之,我擔心方法1不會通過UI進行測試。

+0

嘿,你是唯一一個選擇了黃瓜場景的人(2.例子)我向我們團隊提出了:)。在我看來,一次*通過UI *非常重要。如果我不知道,我應該如何知道要填寫哪些字段以及應用程序如何對無效輸入等作出反應?我可以在我的步驟中嵌入這些內容,但是寫出更糟糕的是(沒有很好的「填寫以下內容」),它也會從文檔中丟失。在成功的功能中,我不會再一次嘗試所有的步驟。相反,我會使用「給定用戶...存在」步驟,該步驟使用工廠。 – gucki 2011-04-05 11:59:38

13

編寫Cucumber測試的要點是創建一個關於您的團隊中無法讀取代碼的人員可以讀取代碼的規範。我會先問他們哪一個更喜歡。

我的猜測是他們會更喜歡第一個,因爲它更具說明性。因爲它是在更高的抽象層次上編寫的(而不是關心點擊頁面上的小部件),所以它更易於閱讀。

與喬什說的相反,我認爲有步驟可以通過用戶界面或無法工作是一個非常好的主意。曲面UI對功能的擔憂可能會使設計更改變得很脆弱,而且閱讀起來也很枯燥。

我最近做了一個談論這個問題上,我認爲這是你在哪裏真正相關: http://skillsmatter.com/podcast/agile-testing/refuctoring-your-cukes

又見這些相關的博客文章:

祝你好運!

+1

感謝您的回答。我觀看了你的演講,並且我並不確信你的功能/場景如何。 7點30分,您正在撰寫名爲「註冊」的功能,其中包含「申請賬戶」,「確認賬戶」和「填寫賬戶資料」等場景。在我的選擇中,這些不是情景,而是單獨的功能。有了「創建功能」,我可以準確地說明創建帳戶時可能發生的情況(提交有效數據,提交無效數據,在一段時間內創建太多帳戶時阻止IP,...)。 – gucki 2011-04-05 11:47:32

+1

...繼續:伊莫「確認帳戶」是一個額外的步驟/功能,需要解釋(爲什麼我要爲什麼用戶要確認他們的帳戶?)。我也可以在這裏添加不同的方案:帳戶確認已過期,提交必須填寫的表格等。 – gucki 2011-04-05 11:51:27

5

如果按照黃瓜製造商的標準行事,第一套就是明確的贏家。

這篇博客文章,"The training wheels came off"作者是(黃金製造商之一)(AslakHellesøy)的作者之一,主要針對你寫的兩個選項之間的區別。毫不含糊地說,他解釋說,以陳述性,領域爲中心,易於理解的特徵是黃瓜用戶應該努力達到的目標,並且使得你的選擇1成爲贏家。

他基本上說,努力實現在系統語言DRY 功能步驟(點擊此,選擇等)黃瓜代碼氣味。用他的話說:

如果你需要的是用於驅動鼠標和鍵盤測試工具, 不要用黃瓜。還有其他的工具可以用來完成這個工作,而且這個工具的抽象和打字費用遠低於Cucumber。

順便說一下,這裏很多人都說過,黃瓜的目的是爲了讓顧客更容易閱讀和理解。我認爲這是略微偏離目標,理解的難易不是目的。我認爲他們讓Cucumber確保客戶和開發人員使用相同的語言並努力獲得相同的結果。因此,讓開發人員進入客戶的行列就像幫助客戶瞭解開發流程一樣。