2017-04-21 104 views
2

如何在多場景中運行黃瓜中的特定場景?如何在黃瓜中運行特定場景

特性文件

Feature: Test Milacron Smoke scenario 

    Scenario: Test login with valid credentials 
    Given open firefox and start application 
    When I click on Login 
    And enter valid "[email protected]" and valid "[email protected]" 
    Then Click on login and User should be able to login successfully 

    Scenario: Test shop for cart 
    Given Click on shop for carts 
    And select plates 
    When Click on Add to cart 
    Then product should be added in the cart successfully 
    And verify the product 

Scenario: Test login with valid credentials1 
    Given open firefox and start application 
    When I click on Login 
    And enter valid "[email protected]" and valid "[email protected]" 
    Then Click on login and User should be able to login successfully 

    Scenario: Test shop for cart1 
    Given Click on shop for carts 
    And select plates 
    When Click on Add to cart 
    Then product should be added in the cart successfully 
    And verify the product 

測試運行

package runner; 

import org.junit.runner.RunWith; 

import cucumber.api.junit.Cucumber; 

@RunWith(Cucumber.class) 
@Cucumber.Options(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"}) 

public class TestRunnr { 

} 

回答

0

您需要使用標籤來過濾掉的情況。將標籤放在特徵文件中,並將其添加到跑步者的黃瓜選項中。

@RunScenarioExample 
Scenario: Test login with valid credential 

@Cucumber.Options(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"}, tags={"@RunScenarioExample"}) 
1

在下面的黃瓜中使用標籤未來。

Feature: Test Milacron Smoke scenario 

    @Test1 
    Scenario: Test login with valid credentials 
    Given open firefox and start application 
    When I click on Login 
    And enter valid "[email protected]" and valid "[email protected]" 
    Then Click on login and User should be able to login successfully 

    @Test2 
    Scenario: Test shop for cart 
    Given Click on shop for carts 
    And select plates 
    When Click on Add to cart 
    Then product should be added in the cart successfully 
    And verify the product 

    @Test3 
    Scenario: Test login with valid credentials1 
    Given open firefox and start application 
    When I click on Login 
    And enter valid "[email protected]" and valid "[email protected]" 
    Then Click on login and User should be able to login successfully 

    @Test4 
    Scenario: Test shop for cart1 
    Given Click on shop for carts 
    And select plates 
    When Click on Add to cart 
    Then product should be added in the cart successfully 
    And verify the product 

如果您只想運行Test1場景更新運行程序文件,如下所示。

import org.junit.runner.RunWith; 

import cucumber.api.CucumberOptions; 
import cucumber.api.junit.Cucumber; 

@RunWith(Cucumber.class) 
@CucumberOptions(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"},tags={"@Test1"}) 
public class TestRunner { 

} 

如果要執行多個場景,請保留逗號分隔標籤,如下所述。

import org.junit.runner.RunWith; 

    import cucumber.api.CucumberOptions; 
    import cucumber.api.junit.Cucumber; 

    @RunWith(Cucumber.class) 
    @CucumberOptions(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"},tags={"@Test1,@Test2"}) 
    public class TestRunner { 

    } 
2

像其他答案建議,使用標籤。如果你使用Maven的時候,你不用改變轉輪文件 - 只需添加到您的通話行家

-Dcucumber.options="--tags @Test1" 

我喜歡這個方法的事情是,我不會冒險在亞軍犯下的標籤文件。

此外,這裏是一個在maven中運行多個標籤的例子。

0

您可以在使用標籤的功能中使用選擇性功能文件或選擇性場景。請嘗試使用此解決方案。

讓我們考慮你有n個特徵文件,你只需要從中運行選擇性特徵。然後用@tag名稱命名每個功能文件。

例如:在此文件夾,如果你有特點n個 - 「的src /主/資源/發佈」

1功能文件名:

Login.feature

//文件裏面開始與功能變量名稱

@Login 
Feature: To Login to Email 

//Then feature name followed with scenario tag name 
@User1 

#Scenario1: 
Scenario Outline: Navigate and logon to gmail application 

Given User launches gmail application 
When User updates emailID <emailID> 
And User updates pwd <pwd>  
Then User clicks on Login Button 


Examples: 
    | emailID | pwd | 
    | [email protected]| 123 | 


@User2 

#Scenario2: 
Scenario Outline: Navigate and logon to facebook application 

//Write the code for scenario 2 as similar to above 

第二個特點文件名:

CreateEmail.feature

@Createmail 
Feature: Create email 

Scenario: Blah blah blah... 

//Write all Given when And Then 

第三特徵文件名:

SendEmail.feature

@Sendemail 
Feature: Send email 

Scenario: Blah blah blah... 

//Write all Given when And Then 

所以從上面的測試文件。讓我們考慮要單獨測試1和第3的功能,然後你可以使用如下代碼:

例如:

//這是運行特定功能的文件,這是1和3。同樣,你可以如果在同一個特徵文件中有n個編號場景,也可以使用標籤進行場景。

@CucumberOptions(features= "src/main/resources/publish", tags="@Login, @Sendemail", format = {"pretty"}) 

//這是在特徵文件中運行特定場景。如果您有多個場景,那麼您可以編寫指定您的場景標籤和逗號。

@CucumberOptions(features= "src/main/resources/publish/Login.feature", tags="@User2", format = {"pretty"})