如何使用Gradle运行cucumber-jvm测试

时间:2012-02-15 11:17:19

标签: cucumber gradle

我正在尝试使用新的Cucumber-jvm系统和Gradle作为我的构建系统。

我在GitHub cucumber-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的新手!!

3 个答案:

答案 0 :(得分:4)

我记得Gradle和Cucumber与junit runner有问题。 我最终放弃并使用命令行运行器创建了一个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更新

随着时间的推移,有一些小的变化:

  • cli的包名称已更改为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'] 
}

答案 1 :(得分:0)

其他方法可以是创建任务并包含测试的跑步者类

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

答案 2 :(得分:0)

考虑:

  • 您的derived2文件位于Final
  • 您的胶水类别在.feature

然后,按照explained in the docs进行操作,您可以在src/test/resources/cucumber/features中进行操作:

com.example.myapp.glue

现在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 test批注中的内容。更多详细信息:https://cucumber.io/docs/cucumber/api/#list-configuration-options

相关问题