繁体   English   中英

如何使用 gradle 运行黄瓜测试

[英]How to run the cucumber test using gradle

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

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

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

  1. 在 gradle 中调用 TestNGCucumberRunner.java

或者

  1. 调用gradle中的所有功能

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

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

更新:

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

构建.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

}

目录结构

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

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


老的

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

构建.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']
}

目录布局

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

命令

gradle cucumber

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

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

但这里是总结:

构建.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'
    )
}

你可以阅读官方文件。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM