运行Cucumber 项目上的 ning mvn 没有 运行 任何测试

Running mvn on Cucumber Project Doesn't Run Any Tests

我在 Eclipse 中有一个 Cucumber Java 项目。当我尝试 运行 maven 时,无论是在 eclipse 中还是从命令行,都没有测试 运行。但是当我 运行 使用 Junit 在 eclipse 中进行测试时(右键单击 -> 运行 使用 Junit),它 运行 是测试。我研究了很多关于这个确切问题的文章,但是 none 提出的解决方案解决了我的问题。 这是我的项目结构的描述: 源文件位于 src/main/java/linkedinlearning/cucumbercourse/*

功能文件位于 2 个不同的位置(稍后我会解释原因)

  1. src/test/java/linkedinlearning/cucumbercourse/MenuManagement.特征
  2. src/test/resources/features/MenuManagement.特征

步骤定义文件位于: src/test/java/stepdefinitions/MenuManagementSteps.java

测试 运行ner 文件位于 src/test/java/testrunners/MenuManagementTest.java

我把特征文件放在了两个不同的地方,因为最初它只在 src/test/java/linkedinlearning/cucumbercourse/ 但后来我在网上看到它应该放在 src/test/resources/features,所以我也把它加在那里。

这是我的 pom 文件:

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>linkedinlearning</groupId>
    <artifactId>cucumbercourse</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <name>Cucumbercourse</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>13</java.version>
        <java.home>/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home</java.home>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>5.8.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-bom</artifactId>
                <version>7.2.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit-platform-engine</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.0</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <properties>
                        <!-- Work around. Surefire does not include enough
                             information to disambiguate between different
                             examples and scenarios. -->
                        <configurationParameters>
                            cucumber.junit-platform.naming-strategy=long
                        </configurationParameters>
                    </properties>
                    <includes>
                     
                    <include>MenuManagementTest.java</include>
                        
                </includes>
                <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
            <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
        </plugins>
    </build>
</project>

这是我的测试运行ner 文件MenuManagementTest.java

package testrunners;


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


@RunWith(Cucumber.class)
@CucumberOptions(
        features="src/test/resources/features",
        glue= "stepdefinitions",
        plugin= {"pretty"})
    
public class MenuManagementTest {
}

我将 $JAVA_HOME 设置为指向正确的 JDK (/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home)。这是来自命令行运行ning“mvn clean test”的结果

jeffmartin@pc-jmartin ~/eclipse-workspace/cucumbercourse % mvn clean test
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< linkedinlearning:cucumbercourse >-------------------
[INFO] Building Cucumbercourse 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ cucumbercourse ---
[INFO] Deleting /Users/jeffmartin/eclipse-workspace/cucumbercourse/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ cucumbercourse ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/jeffmartin/eclipse-workspace/cucumbercourse/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.0:compile (default-compile) @ cucumbercourse ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/jeffmartin/eclipse-workspace/cucumbercourse/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ cucumbercourse ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.0:testCompile (default-testCompile) @ cucumbercourse ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/jeffmartin/eclipse-workspace/cucumbercourse/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ cucumbercourse ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.661 s
[INFO] Finished at: 2022-02-17T15:47:32-05:00
[INFO] ------------------------------------------------------------------------

是否有明显的原因导致测试被忽略?

谢谢

您使用的 JUnit 5 与 Maven Surefire(开发速度缓慢)相比相对较新。所以旧版本的 Surefire 还不知道如何 运行 JUnit 5 测试。

不过谜题比较多。您还使用了 cucumber-junit,它使用 JUnit 4。但是您没有 junit-vintage 到 运行 它与 JUnit 5。

为了避免更多的混乱,我建议删除所有内容并从https://github.com/cucumber/cucumber-java-skeleton重新开始。


Maven 仅将文件从 src/**/resources 复制到 target/classestaret/test-classessrc/**/java 中的 .java 个文件首先被编译,其他文件大多被忽略。

所以 .feature 其他地方的文件不会被复制,应该被删除。如有疑问,请查看这些文件夹的内容。

我还建议将所有内容放在一个包中并遵循命名包的约定 https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

请注意,包是 src/**/javasrc/**/resources 之后的任何内容。如有疑问,请再次查看 target/classestaret/test-classes 文件夹。


The source files are located in src/main/java/linkedinlearning/cucumbercourse/*

你是我见过的第 4 个使用该课程但卡住了的人。

看看https://cucumber.io/docs/guides/10-minute-tutorial/