首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Gradle运行cucumber-jvm测试

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

Stack Overflow用户
提问于 2012-02-15 19:17:19
回答 3查看 11K关注 0票数 8

我正在尝试使用新的Cucumber-jvm系统和Gradle作为我的构建系统来启动一个项目。

我在GitHub cucumber-jvm项目(https://github.com/cucumber/cucumber-jvm)中使用了示例Java代码。

我的项目是在IntelliJ中设置的,并且能够运行测试。

但是,Gradle找不到任何要运行的测试。我之所以知道这一点,是因为我考砸了,而Gradle什么也没说。它在工作时也没有显示任何内容。

它试图运行的类如下所示:

代码语言:javascript
复制
import cucumber.junit.Cucumber;
import cucumber.junit.Feature;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@Feature(value = "CarMaintenance.feature")
public class FuelCarTest {
}

我对黄瓜和Gradle都是新手!

EN

回答 3

Stack Overflow用户

发布于 2013-12-23 20:50:16

我记得Gradle和Cucumber在junit runner上遇到了麻烦。我最终放弃了,使用命令行运行程序创建了一个gradle任务。

代码语言:javascript
复制
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']
}

用于html报告输出的-f文件夹

粘合/步骤代码的-g包名称

要素文件所在的src/test/resources/features

具有以下依赖项

代码语言:javascript
复制
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
  • The标志-f似乎不再起作用,并导致错误

因此,我在build.gradle中完成了以下任务定义

代码语言:javascript
复制
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'] 
}
票数 5
EN

Stack Overflow用户

发布于 2018-11-27 18:15:18

另一种方法是创建任务并包含用于测试的runner类

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2020-07-24 06:56:57

考虑:

  • .feature文件在src/test/resources/cucumber/features

中,而glue类在com.example.myapp.glue

然后,在explained in the docs之后,您可以在build.gradle中执行以下操作

代码语言:javascript
复制
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还将运行黄瓜测试。

args部件支持运行器的@CucumberOptions注释中的内容。更多细节:https://cucumber.io/docs/cucumber/api/#list-configuration-options

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9292311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档