Java 如何使用 gradle 运行黄瓜测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28387045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:07:39  来源:igfitidea点击:

How to run the cucumber test using gradle

javacucumber-jvm

提问by user3350712

I'm having trouble running Cucumber tests in Gradle. I am using cucumber-jvm.

我在 Gradle 中运行 Cucumber 测试时遇到问题。我正在使用黄瓜 jvm。

Class TestNGCucumberRunnerextends the AbstractTestNGCucumberTestsand testng annotations with @beforesuite, @aftersuite..

类使用, ..TestNGCucumberRunner扩展AbstractTestNGCucumberTests和 testng 注释。@beforesuite@aftersuite

I usually run the TestNGCucumberRunner.javain IntelliJ by right-click and it runs successfully. Now I want to

我通常TestNGCucumberRunner.java通过右键单击在 IntelliJ 中运行它,它运行成功。现在我想

  1. Invoke the TestNGCucumberRunner.java in gradle
  1. 在 gradle 中调用 TestNGCucumberRunner.java

or

或者

  1. Invoke all the features in gradle
  1. 调用gradle中的所有功能

I tried to execute the TestNGCucumberRunner.java as a javaexec but that fails.

我试图将 TestNGCucumberRunner.java 作为 javaexec 执行,但失败了。

I tried to execute all the feature files in the package. I have used apply plugin: 'com.github.samueltbrown.cucumber'also.

我试图执行包中的所有功能文件。我也用过apply plugin: 'com.github.samueltbrown.cucumber'

回答by Carlo Bellettini

UPDATE:

更新:

My newsetup uses a different pluginthat supports parallel execution of scenarios, better reporting, and is still actively maintained:

我的设置使用了一个不同的插件,该插件支持场景的并行执行、更好的报告,并且仍在积极维护:

build.gradle

构建.gradle

plugins {
  ...
  id "com.commercehub.cucumber-jvm" version "0.11"
}

addCucumberSuite 'cucumberTest'

dependencies {
  ...
  cucumberTestCompile 'info.cukes:cucumber-java:1.2.5'  // or -java8 if you prefer lambda notation

}

directory structure:

目录结构

└── src
    ├── cucumberTest
    │?? ├── java
    │   │   └── package1 
    │           └──       <- Glue
    │?? └── resources    
    │       └── package2 
    │           └──       <- Features
    ├── main
    │   └── java
    └── test
        └── java

package1and package2names (together with many other options) can be specified in the build.gradle file

package1package2名称(连同许多其他选项)可以在 build.gradle 文件中指定



OLD

老的

My previoussetup for using cucumber for java with gradle.

之前在 gradle 中使用 Cucumber for java 的设置。

build.gradle

构建.gradle

plugins {
    id "java"
    id "com.github.samueltbrown.cucumber" version "0.9"
  }   

dependencies {
    cucumberCompile 'info.cukes:cucumber-java:1.2.4'
}

cucumber {
    formats = ['pretty','junit:build/cucumber.xml']
}

Directory layout

目录布局

└── src
    ├── cucumber
    │?? ├── java         <- Glue
    │?? └── resources    <- Features
    └── main
    │    └── java
    └── test
        └── java

Command

命令

gradle cucumber

回答by Miguel Reyes

I opted for not using com.github.samueltbrown.cucumberplugin, it was last updated in Aug 2015. Instead I created a "integrationTest" (cucumber) sourceSet that I can build independently. I.e, a simple gradle buildwill build testand integrationTestsource sets. If I only want to run integration tests I can run gradle integrationTest -x test, or I can run only test with gradle test -x integrationTest.

我选择不使用com.github.samueltbrown.cucumber插件,它最后一次更新是在 2015 年 8 月。相反,我创建了一个我可以独立构建的“integrationTest”(cucumber)源集。即,一个简单的gradle build将构建testintegrationTest源集。如果我只想运行集成测试,我可以运行gradle integrationTest -x test,或者我只能使用gradle test -x integrationTest.

The steps I followed are here: http://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/

我遵循的步骤在这里:http: //www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/

But here is the summary:

但这里是总结:

build.gradle

构建.gradle

sourceSets {
    integrationTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir file('src/integrationTest/java')
        }
        resources.srcDir file('src/integrationTest/resources')
    }
}

//Ensure that the integrationTestCompile/integrationTestRuntime configuration contains the dependencies that are required to compile/run our unit tests.
configurations {
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime
}

task integrationTest(type: Test) {
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath

    // Gradle skips tasks whose input and output are up to date.
    // To ensure that your integration tests are run every time,
    // tell Gradle that the outputs of the integrationTest task should always be considered out of date.
    outputs.upToDateWhen { false }
}

// Ensure that our integration tests are run before the check task and that the check task fails the build if there are failing integration tests.
// Ensure that our unit tests are run before our integration tests. This guarantees that our unit tests are run even if our integration tests fails.
check.dependsOn integrationTest
integrationTest.mustRunAfter test

// Ensure that the HTML reports of unit and integration tests are created to different report
// build/reports/integrationTest directory contains the HTML report that contains the test results of our integration tests.
tasks.withType(Test) {
    reports.html.destination = file("${reporting.baseDir}/${name}")
}

def cucumberVersion = "1.2.4"

dependencies {
    integrationTestCompile(
            'info.cukes:cucumber-core:' + cucumberVersion,
            'info.cukes:cucumber-java:' + cucumberVersion,
            'info.cukes:cucumber-java:' + cucumberVersion,
            'info.cukes:cucumber-junit:' + cucumberVersion,
            'info.cukes:cucumber-spring:' + cucumberVersion,
            'org.springframework:spring-beans:4.2.5.RELEASE'
    )
    integrationTestRuntime(
            'org.springframework:spring-context:4.2.5.RELEASE',
            'org.springframework:spring-test:4.2.5.RELEASE',
            'org.springframework:spring-tx:4.2.5.RELEASE'
    )
}

回答by hnrain

you can read official document.

你可以阅读官方文档。

https://docs.cucumber.io/tools/java/#gradle

https://docs.cucumber.io/tools/java/#gradle

回答by CR Ewert

The cucumber docs do not use the gradle Test task type, so certain integrations are not there (notably Jacoco). Im still hunting for a way to get a Test task to execute cucumbers main when it runs tests.

Cucumber 文档不使用 gradle Test 任务类型,因此不存在某些集成(特别是 Jacoco)。我仍在寻找一种方法来获取测试任务以在运行测试时执行黄瓜主要任务。