简体   繁体   English

如何从Gradle文件中运行黄瓜方案?

[英]How to Run Cucumber Scenarios from Gradle file.?

I have a cucumber+Java project where everything is working perfectly fine if I use JUnit Runner to execute cucumber scenarios written in Feature file but problem arises when I try to use build.gradle file to run them. 我有一个Cucumber + Java项目,如果我使用JUnit Runner执行以Feature文件编写的Cucumber方案,那么一切工作都很好,但是当我尝试使用build.gradle文件运行它们时会出现问题。

@Scenario1
Given I have URL 
When When I login 
Then I can see Homescreen

@Scenario2
Given I am logged in
When I make payment
Then I can see payment receipt

I have created a gradle task- 我创建了一个gradle任务-

task Cucumber()<<{
    println 'Running Test'
    javaexec {
            main = "cucumber.api.cli.Main"
        classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
        args =['--format','pretty','--format',
    'html:'+System.getProperty("port")+System.getProperty("tag"),
    '--format',
    'json:'+System.getProperty("port")+'/cucumber.json',
    'src/test/resources'
    ,'--glue','classpath:stepDefinition',
    '--tags', System.getProperty("tag")]

    }
}

Scenario2 steps are read by Gradle task but at the same time Scenario1 steps are not found. Gradle任务会读取Scenario2步骤,但同时找不到Scenario1步骤。

What could be the issue? 可能是什么问题?

If you run your cucumber tests from a class called RunCukesTest, it could be as simple as 如果您从名为RunCukesTest的类运行黄瓜测试,则可能很简单

gradle -Dtest.single=RunCukesTest < modulename > :test gradle -Dtest.single = RunCukesTest <模块名称>:test

The Way I did is following: 我的方法如下:

1) Create a separate folder for your integration test, this is preferred as your integration test runs for a long time and you don't want to run them every time you build. 1)为您的集成测试创建一个单独的文件夹,这是首选,因为您的集成测试可以运行很长时间,并且您不想每次构建时都运行它们。

2) Add following code to your build.gradle: 2)将以下代码添加到您的build.gradle中:

configurations {
    intTestCompile.extendsFrom testCompile
    intTestRuntime.extendsFrom testRuntime

}

To add in sourcesets: 要添加源集:

sourceSets {
    main {
        java {
            srcDirs = ["$projectDir/src/main/java"]
        }
    }
    test {
        java {
            srcDirs = ["$projectDir/src/test/java"]
        }
    }
    intTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir file('src/intTest/java')
        }
        resources.srcDir file('src/intTest/resources')
    }
}

Task to run integration test: 运行集成测试的任务:

task intTest(type: Test) {
    description = "Run integration tests (located in src/intTest/...)."
    setTestClassesDirs(project.sourceSets.intTest.output.classesDir)
    classpath = project.sourceSets.intTest.runtimeClasspath
    outputs.upToDateWhen { false }

    testLogging {
        events "PASSED", "STARTED", "FAILED", "SKIPPED"
    }
}

Now just execute the following: 现在只需执行以下命令:

gradle intTest

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

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