黄瓜测试未运行

时间:2017-05-14 17:07:54

标签: java selenium cucumber

我正在开发我的第一个功能文件/ selenium项目。

我创建了一个功能文件和跑步者类。

package cucumberpkg2;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions
(features="Features")	
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测试运行时,我都会收到错误:

在所选项目中找不到测试类。

2 个答案:

答案 0 :(得分:2)

以下是您的问题的解决方案:

  1. 根据Cucumber的当前文档,您可能需要在Scenario Outline文件中将关键字Scenario更改为test.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. 因此,您将在test.feature子目录中拥有Features文件,其中包含以下代码:

    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. 最后,当您作为JUnit测试执行Runner类时,您将在控制台上看到以下消息:

    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选项,可以轻松完成这些警告。

  10. 如果这回答你的问题,请告诉我。

答案 1 :(得分: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 {
}

您可以使用多个标签