我正在使用 Cucumber 和 JUnit5 为我的项目编写测试。我的项目使用 Spring Framework 和 Gradle 作为构建工具,我使用 IntelliJ Idea 作为具有 Cucumber 和 Gradle 插件的编辑器。这是我的黄瓜跑步者:
package com.mycom.myproject.cucumber;
import static io.cucumber.junit.platform.engine.Constants.*;
import org.junit.platform.suite.api.*;
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("src/test/resources")
@ConfigurationParameters({
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.mycom.myproject.cucumber.steps"),
@ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "src/test/resources/cucumber"),
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "html")
})
public class RunCucumberTest {
}
这是我的 cucumberBootstrap 类:
package com.mycom.myproject.cucumber;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import com.mycom.myproject.Application;
import io.cucumber.spring.CucumberContextConfiguration;
@CucumberContextConfiguration
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, args = "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,com.arkea.catalyst.kafka.spring.KafkaAutoConfiguration")
@ActiveProfiles({ "IC" })
public class CucumberBootstrap {
}
我的步骤定义类:
package com.mycom.myproject.cucumber.steps;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class StepDefinition extends CucumberBootstrap {
@Quand("I want to calculate my bill")
public void iWantToCalculateMyBill() {
// some code
}
@Alors("I have this result")
public void iHaveThisResult() {
// some assertions
}
}
这是我的 gradle.build 文件:
// Tests dependencies
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testImplementation 'org.junit.jupiter:junit-jupiter-engine'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'io.rest-assured:rest-assured:4.1.2'
testImplementation "io.cucumber:cucumber-java:$cucumberVersion"
testImplementation "io.cucumber:cucumber-spring:$cucumberVersion"
testImplementation "io.cucumber:cucumber-junit-platform-engine:$cucumberVersion"
testImplementation "org.junit.vintage:junit-vintage-engine"
testImplementation 'org.junit.platform:junit-platform-suite:1.9.0'
implementation 'org.junit.platform:junit-platform-commons:1.9.0'
}
test {
useJUnitPlatform()
systemProperty 'java.util.logging.manager', 'org.apache.logging.log4j.jul.LogManager'
testLogging.showStandardStreams = true
testLogging.exceptionFormat = 'full'
}
我的cucumber版本是7.5.0。 无论我如何更改代码,我都会遇到“未找到给定包含的测试”错误,并且我不知道如何更改它。 你们有什么线索吗?
谢谢
在你的跑步者课程中,你有两个问题。
@SelectClasspathResource("src/test/resources")
这将选择一个类路径资源。然而
src/test/resources
不是类路径上的资源。
为了解释,请考虑一个简单的项目,您可能具有以下布局:
src
├── main
│ └── java
│ └── io
│ └── cucumber
│ └── skeleton
│ └── Belly.java
└── test
├── java
│ └── io
│ └── cucumber
│ └── skeleton
│ ├── RunCucumberTest.java
│ └── StepDefinitions.java
└── resources
├── io
│ └── cucumber
│ └── skeleton
│ └── belly.feature
└── junit-platform.properties
这里
src/main/java
、src/test/java
和 src/test/resources
是不同的来源。当您构建项目时,这些文件将被编译或复制到 build
文件夹:
build
├── classes
│ └── java
│ ├── main
│ │ └── io
│ │ └── cucumber
│ │ └── skeleton
│ │ └── Belly.class
│ └── test
│ └── io
│ └── cucumber
│ └── skeleton
│ ├── RunCucumberTest.class
│ └── StepDefinitions.class
└── resources
└── test
├── io
│ └── cucumber
│ └── skeleton
│ └── belly.feature
└── junit-platform.properties
然后,在运行测试时,类路径由您的依赖项以及
build/classes/java/{main,test}
和 build/resources/test
类路径根组成。
这意味着
io/cucumber/{Belly,RunCucumberTest,StepDefinitions}.class
以及 io/cucumber/skeleton/belly.feature
都是类路径上的资源。
并且因为
@SelectClasspathResource
选择了类路径资源,所以您应该使用 @SelectClasspathResource("io/cucumber/skeleton/belly.feature")
。如果您有多个功能 @SelectClasspathResource("io/cucumber/skeleton")
也可以使用。
@ConfigurationParameter(key = FEATURES_PROPERTY_NAME, ..)
FEATURES_PROPERTY_NAME
仅存在,因为 Maven 和 Gradle 不支持来自命令行的 JUnit5s 发现选择器。在这种情况下你不需要它。
你已经很接近了!
在您的
RunCucumberTest
班级中:
@SelectClasspathResource("features")
@ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "src/test/resources/cucumber"),
然后将你的功能文件放在:
src/test/resources/features/
顺便说一下,你不需要:
@IncludeEngines("cucumber")
Ever since Junit evolved from version 4 to version 5. There were a lot of very strong and powerful features implemented.
In JUnit 5, the @RunWith annotation has been replaced by the more powerful @ExtendWith annotation. However, the @RunWith annotation can still be used for backward compatibility
Just add the following if one is using gradle and boom your error should be fixed.
testRuntimeOnly("org.junit.vintage:junit-vintage-engine")
For maven add
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>your_version_here</version>
<scope>test</scope>