2017-05-14 223 views
3

我正在處理我的第一個功能文件/硒項目。黃瓜測試沒有運行

我創建了一個功能文件和跑步者類。

package cucumberpkg2; 
 
import org.junit.runner.RunWith; 
 
import cucumber.api.CucumberOptions; 
 
import cucumber.api.junit.Cucumber; 
 
@RunWith(Cucumber.class) 
 
@CucumberOptions 
 
(features="Features") \t 
 
public class Runner { 
 

 
}
我有特徵文件test.feature

Feature: Login screen 

    Scenario Outline: Successful login 
    Given User is on Login page 
    When User enters valid UserName and Password 
    And Clicks the login button 
    Then User landed on the home page 

但每當我嘗試運行的TestRunner類作爲JUnit測試,我得到的錯誤:

測試類沒有在選定的項目中找到。

+1

您還需要在@CucumberOptions – kushal

+0

@kushal中爲特性文件指定glue作爲「glue = {」packagename.classname「}」的步驟定義所在的位置。 'glue'選項不是強制性的,用於測試Cucumber項目綁定。但是當你想測試完整的實現時,'glue'是必須的。謝謝 – DebanjanB

+0

@dev,我知道膠水有時候不是強制性的,但我不明白你的聲明:/ – kushal

回答

0

您需要提供如下所述的功能文件的完整路徑。

@RunWith(Cucumber.class) 
@CucumberOptions(
       features = {"src/test/resources/com/gaurang/steps/demo.feature", 
          "src/test/resources/com/gaurang/steps/demo1.feature" 
          } 
       ) 
public class RunAllTest { 

} 

,或者如果你有太多的功能,文件的最好方法是提供標籤功能文件,然後使用這些標籤如下所述運行。

@userRegistrations 
Feature: User Registration 

RunAllTest.java

@RunWith(Cucumber.class) 
@CucumberOptions(tags={"@userRegistrations"}) 
public class RunAllTest { 
} 

你也可以使用多個標籤

+0

你的解決方案將只處理'demo.feature'(一個)功能文件,如果你有多個功能文件:) – DebanjanB

+1

@Dev我需要多個功能文件,我已經更新了答案。然而,更好的事情是使用標籤。提供標籤功能,然後使用它們。 –

+0

是的,你是對的:)而是我會通過包含功能的文件夾名稱 – DebanjanB

0

這裏是解決你的問題:

  1. 爲每Cucumber當前文檔您可能需要更改關鍵字Scenario OutlineScenariotest.feature文件中。
  2. 雖然你提到TestRunner類,但你的代碼談到Runner類被實現爲public class Runner,可以肯定的類文件,你爲JUnit test執行的哪個。
  3. 您可能需要改變@CucumberOptions@Cucumber.Options以前版本(最新版本的@CucumberOptions工作)
  4. 將下面的兩個部分在一個單一的線@Cucumber.Options (features="Features")
  5. 正如你會被提@Cucumber.Options(features="Features")確保您的特性文件放置在項目目錄內的Features子目錄內。
  6. 那麼你將有內Features子目錄test.feature文件用下面的代碼:

    Feature: Login screen 
        Scenario: Successful login 
        Given User is on Login page 
        When User enters valid UserName and Password 
        And Clicks the login button 
        Then User landed on the home page 
    
  7. Runner類看起來像:

    import org.junit.runner.RunWith; 
    import cucumber.api.junit.Cucumber; 
    @RunWith(Cucumber.class) 
    @CucumberOptions(features="Features") 
    public class Runner { 
    
    } 
    
  8. 最後,當你將執行Runner類作爲JUnit測試,您將在控制檯上看到以下消息:

    You can implement missing steps with the snippets below: 
    
    @Given("^User is on Login page$") 
    public void User_is_on_Login_page() throws Throwable { 
        // Express the Regexp above with the code you wish you had 
        throw new PendingException(); 
    } 
    
    @When("^User enters valid UserName and Password$") 
    public void User_enters_valid_UserName_and_Password() throws Throwable { 
        // Express the Regexp above with the code you wish you had 
        throw new PendingException(); 
    } 
    
    @When("^Clicks the login button$") 
    public void Clicks_the_login_button() throws Throwable { 
        // Express the Regexp above with the code you wish you had 
        throw new PendingException(); 
    } 
    
    @Then("^User landed on the home page$") 
    public void User_landed_on_the_home_page() throws Throwable { 
        // Express the Regexp above with the code you wish you had 
        throw new PendingException(); 
    } 
    
  9. 通過向Cucumber實施glue選項,可以輕鬆地保護這些警告。

讓我知道這個答案是否是您的問題。