在 Cucumber 项目上运行 mvn 不会运行任何测试

问题描述 投票:0回答:2

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

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

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

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

测试运行程序文件位于 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>

这是我的测试运行程序文件 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)。这是从命令行运行“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] ------------------------------------------------------------------------

有什么明显的原因导致测试被忽略吗?

谢谢

java eclipse maven junit cucumber
2个回答
1
投票

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

然而还有更多的谜题。您还使用了使用 JUnit 4 的

cucumber-junit
。但是您没有
junit-vintage
来使用 JUnit 5 运行它。

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


Maven 仅将文件从

src/**/resources
复制到
target/classes
taret/test-classes
。首先编译来自
.java
src/**/java
文件,其他文件大多被忽略。

因此

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

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

请注意,包是

src/**/java
src/**/resources
之后的任何内容。如有疑问,请再次查看
target/classes
taret/test-classes
文件夹。


源文件位于 src/main/java/linkedinlearning/cucumbercourse/*

你是我见过的第四个使用该课程并陷入困境的人。

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


0
投票

我在使用 Spring Boot REST 尝试 Cucumber 时遇到了同样的问题。最后,我在某处找到了解决方案,但不记得在哪里。基本上 junit-jupiter-engine 必须从 spring-boot-starter-test 中排除。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

这是我的演示项目:https://github.com/almarsimon/spring-boot-rest-cucumber

© www.soinside.com 2019 - 2024. All rights reserved.