Cucumber 1.2.4 未找到步骤定义:"You can implement missing steps with the snippets below" (2016)

Cucumber 1.2.4 not finding step definitions: "You can implement missing steps with the snippets below" (2016)

我正在尝试 运行 在类 UNIX 上使用 Maven 的 Cucumber(不幸的是我被迫使用 Windows,虽然 cmd.exe 有相同的结果)命令行:

mvn clean test -Dcucumber.options="src/test/resources/com/example/sqa/automation_cuke/pages/sample_test.feature"

结果:

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
...
[INFO] --- exec-maven-plugin:1.2.1:java (default) @ sqa.automation_cuke ---
Feature: My animals Sample
  Sample test to use as basis for conversion

  Scenario: My animals           # sample_test.feature:4
    Given that I have my animals

1 Scenarios (1 undefined)
1 Steps (1 undefined)
0m0.000s


You can implement missing steps with the snippets below:

@Given("^that I have my animals$")
public void that_I_have_my_animals() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

我查看了所有排名靠前的 Google/SO 搜索以查找此错误消息和类似的错误消息。许多,例如 Cucumber can't find steps when running a single feature and Cucumber not finding step definitions,说要使用 -r 和 --require 参数指定我的步骤定义,但它们似乎在某些时候已从 Cucumber 中删除,因为我得到一个 Unknown option: -r错误。

许多其他答案说当@CucumberOptions glue 参数 定义不正确时会(奇怪地)发生此错误。但是我尝试了以下胶水并且 none 起作用了:

sqa.automation_cuke.pages path:sqa.automation_cuke.pages classpath:sqa.automation_cuke.pages com.example.sqa.automation_cuke.pages classpath:com.example.sqa.automation_cuke.pages path:com.example.sqa.automation_cuke.pages C:/development/cucumber-demo/src/main/java/com/example/sqa/automation_cuke/pages src/main/java/com/example/sqa/automation_cuke/pages classpath:C:/development/cucumber-demo/src/main/java/com/example/sqa/automation_cuke/pages classpath:src/main/java/com/example/sqa/automation_cuke/pages path:src/main/java/com/example/sqa/automation_cuke/pages C:\development\cucumber-demo\src\main\java\com\example\sqa\automation_cuke\pages src\main\java\com\example\sqa\automation_cuke\pages classpath:src\main\java\com\example\sqa\automation_cuke\pages path:src\main\java\com\example\sqa\automation_cuke\pages

我也试过:

这是我的项目结构。我确保资源结构与代码的其余部分匹配 as per this answer:

sample_text.feature:

Feature: My animals Sample
  Sample test to use as basis for conversion

  Scenario: My animals
    Given that I have my animals

MySampleTest.java:

package com.example.sqa.automation_cuke.pages;

@RunWith(Cucumber.class)
@CucumberOptions(glue = {"com.example.sqa.automation_cuke.pages"})
public class MySampleTest {

    @Given("^that I have my animals$")
    public void that_I_have_my_animals() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        System.out.println("MySampleTest.that_I_have_my_animals() ran!");
        new MySample().printMySample();
        throw new PendingException();
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>sqa.automation_cuke</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cucumber-jvm.version>1.2.4</cucumber-jvm.version>
        <selenium.version>2.53.0</selenium.version>
        <junit.version>4.12</junit.version>
    </properties>

    <build>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executableDependency>
                        <groupId>info.cukes</groupId>
                        <artifactId>cucumber-core</artifactId>
                    </executableDependency>
                    <mainClass>cucumber.api.cli.Main</mainClass>
                    <arguments>
                        <argument>--plugin</argument>
                        <argument>junit:target/cucumber-junit-report/allcukes.xml</argument>
                        <argument>--plugin</argument>
                        <argument>pretty</argument>
                        <argument>--plugin</argument>
                        <argument>html:target/cucumber-html-report</argument>
                    </arguments>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>info.cukes</groupId>
                        <artifactId>cucumber-core</artifactId>
                        <version>${cucumber-jvm.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>${cucumber-jvm.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>${cucumber-jvm.version}</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber-jvm.version}</version>
        </dependency>
        <!-- http://mvnrepository.com/artifact/info.cukes/cucumber-java -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber-jvm.version}</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.7</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
</project>

如有任何帮助,我们将不胜感激!

您需要为 cucumber 选项设置 features 属性才能正常工作。

将您的黄瓜选项注释替换为:

@CucumberOptions(features = {"classpath:com/example/sqa/automation_cuke/pages/sample_test.feature"}, glue = {"com.example.sqa.automation_cuke.pages"},

应该可以正常工作。如果没有,请更新 post。

更新:

SampleTestStepDefiniton.java

package com.example.sqa.automation_cuke.pages;

import cucumber.api.java.en.Given;

public class SampleTestStepDefinition {

    @Given("^that I have my animals$")
    public void that_I_have_my_animals() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        System.out.println("MySampleTest.that_I_have_my_animals() ran!");   
    }
}

MySampleTest.java

package com.example.sqa.automation_cuke.pages;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(glue = { "com.example.sqa.automation_cuke.pages" }, features = {
        "classpath:com/example/sqa/automation_cuke/pages/sample_test.feature" })
public class MySampleTest {

}

sample_test.feature

Feature: My animals Sample
  Sample test to use as basis for conversion

  Scenario: My animals
    Given that I have my animals

更新

我认为您项目的问题在于它无法在类路径中找到测试 类(以及步骤定义 类)。我不明白为什么你必须定义一个 CLASSPATH 系统变量。这可能是这个问题的原因。然而,PATH 变量应该保留并且不应包含对特定项目的任何引用

尝试删除 CLASSPATH 变量并用以下内容替换您的插件标签:

 <plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <encoding>utf8</encoding>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>test-jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18</version>
    </plugin>
</plugins>

我可以在多个 window 环境中使用 java 8 和 java 7 获得它 运行。让我 posted!

您的设置非常复杂。想Gall’s Law, I would do a restart and build on something that works. A small getting started project is available at https://github.com/cucumber/cucumber-java-skeleton

Download/Clone 它和 运行

mvn test

这应该会为您提供一个缺失的步骤,但它也应该为您提供一些已定义的步骤。查看源代码并在 src/test/java/skeleton/Stepdefs.java

中添加缺少的步骤

我也遇到了同样的问题,然后我意识到为 runner class 导入的包与 Step 定义不同 class

所以
如果对于 runner class 你正在使用如下的 io 包

import io.cucumber.junit.Cucumber;

import io.cucumber.junit.CucumberOptions;

那么你也应该在步骤定义中使用相同的 io 包

import io.cucumber.java.en.Given;

我导入API包的错误

import cucumber.api.java.en.Given;