多年來,我一直在使用 cucumber/Java 和 JUnit4 (CucumberOptions),在 IntelliJ 和 maven 命令列中運行測驗都沒有問題。
最近,我一直在嘗試遷移到 JUnit5,并且我能夠在 IntelliJ 中運行所有測驗(不幸的是)
我的 POC 專案具有以下結構:
junit5
-Features (folder with feature files)
-resources (folder with files used in tests)
-src
--test
---java
----stepdefs
-----SetupEnvHook
-----StepDefs
----AllTest (testrunner wip)
----JU4Test (testrunner JUnit4)
----JU5Test (testrunner Junit5)
---resources (test resources)
-junit5.iml
-pom.xml
JU5Test.java 檔案:
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.SelectDirectories;
import org.junit.platform.suite.api.Suite;
import stepdefs.SetupEnvHook;
import io.cucumber.java.Before;
import static io.cucumber.core.options.Constants.*;
@Suite
@SelectDirectories("Features")
//@ConfigurationParameter(key = PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME, value = "true")
@ConfigurationParameter(key = PLUGIN_PUBLISH_ENABLED_PROPERTY_NAME, value = "false")
@ConfigurationParameter(key = PLUGIN_PUBLISH_QUIET_PROPERTY_NAME, value = "true")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "json:target/cucumber-reports/cucumber.json")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "stepdefs, my.external.steps.stepdefinition")
public class JU5Test {
@Before
public static void beforeSuite() {
SetupEnvHook.setEnvironment("QA");
}
}
beforeSuite() 方法也用于 JU4Test。
當我在其中設定斷點時SetupEnvHook.setEnvironment("QA");,由于 Before Annotation 不起作用,而在其中設定了另一個斷點,它被完全忽略了
@io.cucumber.java.BeforeAll(order = 9999)
SetupEnvHook 類中的注釋被正確觸發。
我的 pom 檔案如下:
<dependencies>
<dependency>
<groupId>my.external</groupId>
<artifactId>steps</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
請忽略我的外部依賴。這種依賴關系與stepdefinitions測驗運行程式檔案中的粘合屬性有關。
我知道 group 和 version 值也丟失了,但是這些值都是由紅色的相同依賴項提供的,以便更好地控制每個人使用的版本。這一切都在 Java 8 中使用
org.apache.maven.plugins:maven-compiler-plugin:3.10.1
org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M7
io.cucumber:cucumber-java:7.8.1
io.cucumber:cucumber-junit:7.8.1
io.cucumber:cucumber-junit-platform-engine:7.8.1
org.junit.jupiter:junit-jupiter-api:5.9.1
org.junit.jupiter:junit-jupiter-engine:5.9.1
org.junit.platform:junit-platform-suite-api:1.9.1
org.junit.platform:junit-platform-suite-engine:1.9.1
我已經嘗試過使用不同的注釋,不僅來自io.cucumber.java而且來自org.junit(基本上是 JUnit4)并且org.junit.jupiter.api顯然沒有成功。
通過 maven 命令列運行最終得到:
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project junit5: No tests were executed!
然而,它并沒有說“沒有找到測驗”,最初有這個問題并得到了解決。
通過查看錯誤,我懷疑我可能從pom.xmlsurefire插件中丟失了一些東西,但我不知道是什么。(這個 pom 與運行 JU4Test 沒有問題的相同)
還有其他人對我接下來可以嘗試什么有任何想法嗎?或者更好的是,這個 xD 的解決方案
編輯:洗掉影像
uj5u.com熱心網友回復:
然而,它并沒有說“沒有找到測驗”,最初有這個問題并得到了解決。
通過查看錯誤,我懷疑我的 pom.xml surefire 插件中可能缺少某些內容,但我無法弄清楚是什么。(這個 pom 與運行 JU4Test 沒有問題的相同)
根據您的描述,不可能說出您的專案有什么問題。您的依賴串列包括 POM 中未包含的依賴項。
您可能需要考慮從頭開始您的專案。您可以為此使用https://github.com/cucumber/cucumber-java-skeleton。
當我在 SetupEnvHook.setEnvironment("QA"); 由于之前的注釋不起作用,它被完全忽略了
忽略注釋的原因@Before是因為您在不屬于粘合路徑的類上使用了 Cucumber 注釋。
盡管我懷疑您正在嘗試查找 JUnit 4s 的映射@BeforeClass。目前在 JUnit 5s Suite Engine 中沒有這樣的東西。如果您需要它,您應該考慮提出拉取請求。
或者,您可以為每個環境創建一個包含單個類的包,并使用 Cucumbers@BeforeAll掛鉤來設定環境。然后為每個@Suite配置不同的粘合路徑以包含這些鉤子。
盡管我認為從環境變數中讀取目標環境并將其默認設定為正常的東西會更好。然后,您可以為每個環境使用不同的 CI 作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/527019.html
標籤:爪哇行家黄瓜junit5
