[英]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.