2015-05-14 51 views
5

無論如何,我們可以在功能文件中使用if/else概念嗎?例如:如果其他概念在功能文件(小黃瓜語言)中可用嗎?

Scenario: User should be able to check login page 
    Given I am on login page 
    When I click on SignIn button 
    Then I should be in home page 
    If yes 
    Then I will create a new profile 
    Else 
    Then I will logout from page 
+1

沒有,在你的榜樣,你正在一箇中描述兩種情況。他們應該分開。 – jmccure

回答

7

不是我所知道的。小黃瓜(和黃瓜)最好在他們指定謹慎的商業案例時使用,並且應該是可重複的,否則他們很難遵循和測試。看起來你至少有兩個故事在這裏:

Scenario: A new user should be asked to sign in 
    Given I am a new user 
    And I navigate to the login page 
    When I click on SignIn button 
    I should not be able to get to the home page 

Scenario: An existing user should be able to log in 
    Given I am an existing user 
    And I navigate to the login page 
    And I submit valid credentials 
    When I click on SignIn button 
    I should be taken to the home page 
4

不,你不能,你不應該。功能文件用於商業行爲,而不是編程。

從你的情況我認爲你正試圖處理不同的行爲,取決於你是否註冊。要做到這一點,你會寫兩個場景

Given I am registered 
When I 
Then I should .... 

Given I am a new user 
When I ... 
Then I should be asked to register 

請注意這些場景如何不描述'如何'完成任何事情。任何像'我點擊foo'的特徵都是一種氣味,應該避免。

-1

您可以使用功能文件中的參數並根據傳遞的參數實現代碼中的If else。

0

如果我們在煙霧測試類型的情況下使用小黃瓜並且我們只需要使用UI來確保數據庫中存在某些東西,那麼呢?

Scenario: I need to create one (and only one) Box before I run the rest of my smoke tests 
Given I login as Admin 
When I am on the Box list Page 
Then the test passes if the Box named "QA SmokeTest" exists 
When I click the Add New Box Button 
And enter the details for a New Box 
And press Save New Box 
Then the test passes if the Box named "QA SmokeTest" exists 

重新使用相同的Then步驟兩次基本上是一個if-else的,這將確保我的盒子存在,這樣我可以在煙霧測試套件,需要一個盒子運行我的其他測試。

但是,這是依賴於能夠阻止腳本執行的測試跑步或做喜歡的事,多餘的:
ScenarioContext.Current["TestPassed"] = true;
,然後在每個步驟
if(ScenarioContext.Current.Get<bool>("TestPassed")) return;