我在 Eclipse 中有一個 Cucumber Java 專案。當我嘗試在 eclipse 中或從命令列運行 maven 時,沒有運行任何測驗。但是當我在 Eclipse 中使用 Junit 運行測驗時(右鍵單擊 -> 使用 Junit 運行),它會運行測驗。我已經研究了很多關于這個確切問題的文章,但沒有一個提出的解決方案能解決我的問題。這是我的專案結構的描述:源檔案位于 src/main/java/linkedinlearning/cucumbercourse/*
功能檔案位于 2 個不同的地方(我稍后會解釋原因)
- src/test/java/linkedinlearning/cucumbercourse/MenuManagement.feature
- src/test/resources/features/MenuManagement.feature
步驟定義檔案位于:src/test/java/stepdefinitions/MenuManagementSteps.java
測驗運行器檔案位于 src/test/java/testrunners/MenuManagementTest.java
我將功能檔案放在 2 個不同的地方,因為最初它只在 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] ------------------------------------------------------------------------
是否有明顯的原因導致測驗被忽略?
謝謝
uj5u.com熱心網友回復:
您使用的 JUnit 5 與 Maven Surefire(開發速度緩慢)相比相對較新。所以舊版本的 Surefire 還不知道如何運行 JUnit 5 測驗。
然而還有更多的謎題。您也在使用cucumber-junitwhich 使用 JUnit 4。但您不必junit-vintage使用 JUnit 5 運行它。
為避免更多混亂,我建議洗掉所有內容并從https://github.com/cucumber/cucumber-java-skeleton重新開始。
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/classesandtaret/test-classes檔案夾。
源檔案位于 src/main/java/linkedinlearning/cucumbercourse/*
你是我見過的第四個使用該課程并被卡住的人。
看看https://cucumber.io/docs/guides/10-minute-tutorial/
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429866.html
