Cucumber 在 运行 代码中找不到 StepDefinitions 但手动使用选项 "Find Step" 可以找到步骤定义

Cucumber cannot find StepDefinitions while running the code but manually using the option "Find Step" can find step definition

在如下定义的项目结构中,

当我 运行 来自 RunCukesTest.java using RunAs --> JUnit Test, 的步骤定义无法被 运行ner

找到时

当我点击查找步骤时,打开了正确的文件。无法理解问题出在哪里,因为代码是 运行ning 几天前的。文件从这里下载

https://drive.google.com/open?id=0B4SgyzyvwKhiVTRmRDZuNXNTSjA

亚军class代码

package helpers;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
//features= "src/test/resources/features/navigation",

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"classpath:features"},
        plugin = {"pretty", "html:target/cucumber-html-report"},
        tags = {"@OnlyOneTime"},
//      dryRun = true,
        monochrome = true
        )
public class RunCukesTest{

}

我解决了你的问题,根据你的跑步者 class,胶水路径没有设置。请设置粘合路径。glue={"stepDefinitions"}

package helpers; 
import org.junit.runner.RunWith; 
import cucumber.api.CucumberOptions; 
import cucumber.api.junit.Cucumber; 
//features= "src/test/resources/features"@RunWith(Cucumber.class) @CucumberOptions( 
features = {"classpath:features"}, glue={"stepDefinitions"},plugin = {"pretty", "html:target/cucumber-html-report"}, tags = {"@OnlyOneTime"}, // dryRun = true, monochrome = true ) 
public class RunCukesTest{ }

运行 它作为 Cucumber 的一个特性,它运行良好,但是如果我提供 glue={"stepDefinitions"} 并尝试 运行 它来自 runner 然后 NullPointerException 被抛出,

这个问题是由于没有找到钩子引起的。但是如果我将@Before 和@After 移动到 SDLogin 类,那么它运行良好。

胶水代码应该有钩子和步骤定义的路径

所以修改为glue={"helpers","stepDefinitions"}而不是 glue={"helpers","classpath/stepDefinitions", "classpath/stepDefinitions.LogIn","classpath/stepDefinitions.Publish"}

请参考这个linkSimilar issue on github

就我而言, 功能文件位于 src/test/resources/Features/login.feature 步骤定义在 com.steps.definitions

所以像下面这样解决了这个问题。

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"src/test/resources/Features/login.feature"}, glue={"com.steps.definitions"},plugin = {"pretty", "html:target/cucumber-html-report"})
public class TestRunner {
}

这里是folder/package结构。