【问题标题】:Cucumber feature file not executing from Maven未从 Maven 执行的黄瓜功能文件
【发布时间】:2021-05-26 21:30:07
【问题描述】:

我正在尝试使用 maven 命令 mvn clean install 执行我的 cucumber feature 文件,但它没有选择我的测试类。我可以使用我的 IDE IntelliJ 运行功能文件,但不能从命令行运行。请找到我的代码和 maven 依赖项。

我在这里缺少什么,为什么 mvn clean install 没有选择RunTest 并从这里MyStepdefs 执行步骤定义。

任何帮助将不胜感激,过去两天我一直在苦苦挣扎 - 不知道我在这里做错了什么。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>6.6.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>6.6.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>6.6.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>datatable</artifactId>
            <version>3.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit-platform-engine</artifactId>
            <version>6.6.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <properties>
                        <configurationParameters>
                            cucumber.plugin=pretty,html:target/cucumber.html
                            cucumber.publish.quiet=true
                            cucumber.publish.enabled=false
                        </configurationParameters>
                    </properties>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

test.feature

Feature: To retrieve the customer with customer details

  Scenario: retrieve the customer with customer id
    Given the customer saved with customer name "john" and customer id 100
    When the client calls GET "/customer/{customerId}" with customer id as 100

RunTest.java

import io.cucumber.junit.platform.engine.Cucumber;
    
    @Cucumber
    public class RunTest {
        
    }

MyStepdefs.java

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

public class MyStepdefs {
    @Given("the customer saved with customer name {string} and customer id {int}")
    public void the_customer_saved_with_customer_name_and_customer_id(String string, Integer int1) {
        System.out.println("Entering into given");
    }

    @When("the client calls GET {string} with customer id as {int}")
    public void the_client_calls_get_with_customer_id_as(String string, Integer int1) {
        System.out.println("Entering into when");
    }

}

CucumberSpringConfig.java

import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;


@CucumberContextConfiguration
@SpringBootTest(classes = DemoApplication.class)
public class CucumberSpringConfig {
}

这是我的文件夹结构: Folder Structure

【问题讨论】:

    标签: java maven automated-tests cucumber junit5


    【解决方案1】:

    https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine#use-the-cucumber-annotation

    Cucumber 将扫描带有 @Cucumber 注释的类的包以查找功能文件。 要使用此功能,请将 @Cucumber 注释添加到测试运行程序。这样做将使 Cucumber 运行包含测试运行器的包中的功能文件。

    因此,因为您的注释类在 com.example.demo 包中,所以将功能放在 src/test/resources/com/example/demo 中。

    【讨论】:

    • 非常感谢!它有效,是否有任何选项可以配置我们的功能文件所在的位置?像这样的东西 @CucumberOptions(features = "src/test/resources/features") 我试过了,但 @CucumberOptions 不起作用。请建议是否有任何替代方案。再次感谢!
    猜你喜欢
    • 2022-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    相关资源
    最近更新 更多