[英]How to run cucumber-jvm tests using Gradle
我正在尝试使用新的 Cucumber-jvm 系统和 Gradle 作为我的构建系统来启动一个项目。
我在 GitHub 黄瓜-jvm 项目( https://github.com/cucumber/cucumber-jvm )中使用了示例 Java 代码。
我的项目是在 IntelliJ 中设置的,IDE 能够运行测试。
但是,Gradle 找不到任何要运行的测试。 我知道这一点是因为我通过了测试而 Gradle 什么也没说。 它在工作时也什么也没说。
它试图运行的类如下所示:
import cucumber.junit.Cucumber;
import cucumber.junit.Feature;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@Feature(value = "CarMaintenance.feature")
public class FuelCarTest {
}
我是黄瓜和 Gradle 的新手!!
我记得在使用 junit runner 时遇到了 Gradle 和 Cucumber 的问题。 我最终放弃并使用命令行运行程序创建了一个 gradle 任务。
task executeFeatures(type: JavaExec, dependsOn: testClasses) {
main = "cucumber.cli.Main"
classpath += files(sourceSets.test.runtimeClasspath, file(webAppDir.path + '/WEB-INF/classes'))
args += [ '-f', 'html:build/reports/cucumber', '-g', 'uk.co.filmtrader', 'src/test/resources/features']
}
-f
用于 html 报告输出的文件夹
-g
胶水/步骤代码的包名
src/test/resources/features
特征文件在哪里
具有以下依赖项
testCompile 'org.mockito:mockito-all:1.9.5',
'junit:junit:4.11',
'org.hamcrest:hamcrest-library:1.3',
'info.cukes:cucumber-java:1.0.14',
'info.cukes:cucumber-junit:1.0.14',
'info.cukes:cucumber-spring:1.0.14'
4.2.5 版更新
随着时间的推移发生了一些小的变化:
cucumber.api.cli.Main
-f
似乎不再起作用并导致错误所以我在build.gradle
得到了以下任务定义:
task executeFeatures(type: JavaExec, dependsOn: testClasses) {
main = "cucumber.api.cli.Main"
classpath += files(sourceSets.test.runtimeClasspath)
args += [ '-g', 'uk.co.filmtrader', 'src/test/resources/features']
}
另一种方法可以是创建一个任务并包含用于测试的运行器类
build.gradle-
task RunCukesTest(type: Test) << {
include "RunCukesTest.class"
}
testCompile 'io.cucumber:cucumber-java:4.2.0'
testCompile 'io.cucumber:cucumber-junit:4.2.0'
your class -
@RunWith(Cucumber.class)
@CucumberOptions(dryRun = false, strict = true, features = "src/test/resources", glue
= "com.gradle.featuretests",monochrome = true)
public class RunCukesTest {
}
只需点击命令:- gradle RunCukesTest
考虑:
.feature
文件在src/test/resources/cucumber/features
和com.example.myapp.glue
然后,按照docs 中的说明,您可以在build.gradle
执行build.gradle
:
dependencies {
// ...
testImplementation("io.cucumber:cucumber-java:6.2.2")
testImplementation("io.cucumber:cucumber-junit:6.2.2")
testImplementation("io.cucumber:cucumber-junit-platform-engine:6.2.2")
}
configurations {
cucumberRuntime {
extendsFrom testImplementation
}
}
// this enables the task `gradle cucumber`
task cucumber() {
dependsOn assemble, compileTestKotlin
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--strict', '--plugin', 'pretty', '--plugin', 'junit:build/test-results/cucumber.xml', '--glue', 'com.example.myapp.glue', 'src/test/resources/cucumber/features']
}
}
}
// (OPTIONAL) this makes `gradle test` also include cucumber tests
tasks.test {
finalizedBy cucumber
}
现在gradle cucumber
将运行gradle cucumber
测试。
如果你添加了最后一部分, gradle test
也会运行黄瓜测试。
args
部分支持运行程序的@CucumberOptions
注释中的内容。 更多详情: https : //cucumber.io/docs/cucumber/api/#list-configuration-options
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.