【问题标题】:Cucumber test not running黄瓜测试没有运行
【发布时间】:2017-10-13 11:34:36
【问题描述】:

我正在开发我的第一个功能文件/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 测试运行时,我都会收到错误消息:

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

【问题讨论】:

  • 您还需要在@CucumberOptions 中将步骤定义所在的功能文件指定为glue={"packagename.classname"}, 的位置
  • @kushal。 glue 选项对于测试 Cucumber 项目绑定不是必需的。但是当你想测试完整的实现时,glue 是强制性的:) 谢谢
  • @dev,我知道胶水有时不是强制性的,但我不明白你的说法:/
  • 如果资源文件中的结构与 java 中的结构相同,则不需要胶水语句,否则需要它。

标签: java selenium cucumber


【解决方案1】:

这是您问题的解决方案:

  1. 根据Cucumber 的当前文档,您可能需要在test.feature 文件中将关键字Scenario Outline 更改为Scenario
  2. 虽然您提到了TestRunner 类,但您的代码中提到了Runner 类被实现为public class Runner,请确保您以JUnit test 执行哪个类文件。
  3. 对于以前的版本,您可能需要将 @CucumberOptions 更改为 @Cucumber.Options(最近的版本使用 @CucumberOptions
  4. 将以下 2 部分放在一行中,为 @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 选项,可以轻松处理这些警告。

如果这能回答您的问题,请告诉我。

【讨论】:

    【解决方案2】:

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

    @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 {
    }
    

    你可以使用多个标签

    【讨论】:

    • 您的解决方案将只处理demo.feature(一个)功能文件,如果您有多个功能文件怎么办:)
    • @Dev i 需要多个功能文件,我已经更新了答案。然而,更好的办法是使用标签。为特征提供标签,然后使用它们。
    • 是的,你是对的 :) 我会传递包含功能的文件夹的名称
    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2020-03-28
    • 2011-04-10
    相关资源
    最近更新 更多