.feature 文件中的 Junit5 黄瓜 "No definition found for..."

Junit5 Cucumber "No definition found for..." in .feature file

我正在尝试创建一个简单的 Junit5-Cucumber 项目(在 Eclipse 中),它将用于 UI 测试。

我参考了这个 repo:https://github.com/cucumber/cucumber-java-skeleton

问题:在 test_features.feature 文件中找不到 Open the Chrome and launch the application 的定义(错误发生在 GivenWhenThen 语句中。

# test_features.feature
Feature: Reset functionality on login page of Application

    Scenario: Verification of Reset button
    
       Given Open the Chrome and launch the application
        
       When Enter the username and password
        
       Then Reset the credentials
# RunCucumberTest.java
package lpms.cucumber;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;

import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;
import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("lpms/cucumber")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "lpms.cucumber")
public class RunCucumberTest {
}
# StepDefinitions.java
package lpms.cucumber;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class StepDefinitions {
    @Given("^Open the Chrome and launch the application$")
    public void open_the_chrome_and_launch_the_application() throws Throwable
    {
        System.out.println("This step opens the chrome and launches the application");      
    }
    
    @When("^Enter the username and password$")
    public void enter_the_username_and_password() throws Throwable
    {
        System.out.println("This step enters the username and password on the login page");     
    }
     
    @Then("^Reset the credentials$")
    public void reset_the_credential() throws Throwable
    {
        System.out.println("This step clicks on the reset button.");
    }
}

项目结构

IMAGE OF MY PROJECT STRUCTURE

已解决!

这是来自 Eclipse IDE 的警告,可能只是一个错误,因为我仍然可以完成测试。

旁注:学习最新黄瓜的极其有用的指南:https://cucumber.io/docs/guides/10-minute-tutorial/

我在我的项目中遇到了同样的问题,我会post在这里给出我的解决方案。

我用过Eclipse + Java 11 + SpringBoot 2.6.4

pom.xml 依赖关系

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <scope>test</scope>
        <version>7.3.0</version>
    </dependency>
    
   <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <scope>test</scope>
    </dependency>
    
    <dependency>
       <groupId>io.cucumber</groupId>
       <artifactId>cucumber-junit-platform-engine</artifactId>
       <version>7.3.0</version>
       <scope>test</scope>
    </dependency>

pom.xml 构建部分的插件

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <properties>
                    <configurationParameters>
                        cucumber.junit-platform.naming-strategy=long
                    </configurationParameters>
                </properties>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
        </plugin>

之后,我在 src/test/java 中创建了一个名为

的包

filelife/skynet/cucumber

在这个包中,我创建了我的步骤 class 和我的跑步者 class;步骤 class 仅包含一些日志记录说明,尚未验证任何内容。

步骤 class:

    @Slf4j
public class SendMessagesOnServiceLimitsSteps {
    
    @Given("A ServiceLimits Module with PosTXRate of {int} seconds")
    public void a_service_limits_module_with_pos_tx_rate_of_seconds(Integer posTxRate) {
        log.info("ServiceLimits PosTxRate {}", posTxRate);
        System.out.println("Given Step");
    }

    @When("I keyOn the device")
    public void i_key_on_the_device() {
        System.out.println("Given Step");
    }

    @When("i wait for {int} seconds")
    public void i_wait_for_seconds(Integer int1) {
        System.out.println("Given Step");
    }

    @When("i keyOff the device")
    public void i_key_off_the_device() {
        System.out.println("Given Step");
    }

    @Then("PositionData messages should be {int} or {int}")
    public void position_data_messages_should_be_or(Integer int1, Integer int2) {
        System.out.println("Given Step");
    }

    @Then("device log print {string}")
    public void device_log_print(String string) {
        System.out.println("Given Step");
    }

}

我的跑步者测试 class:

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("filelife/skynet/cucumber")
@ConfigurationParameter(
   key = GLUE_PROPERTY_NAME,
   value = "filelife.skynet.cucumber"
)
public class SkynetTest{

}

我还在 src/test/resources 源文件夹中创建了相同的文件夹路径 (filelife/skynet/cucumber),并粘贴了我的 .feature 文件。

最后,我创建了 2 个文件:

cucumber.properties

junit-platform.属性

在同一个源文件夹 src/test/resources 中,它们都包含字符串:

cucumber.publish.quiet=true

此配置适用于:

mvn tests

right click on SkynetTest -> RunAs -> Junit Test