2017-05-14 169 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选项,可以轻松地保护这些警告。

让我知道这个答案是否是您的问题。