简体   繁体   中英

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.

@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-

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.

What could be the issue?

If you run your cucumber tests from a class called RunCukesTest, it could be as simple as

gradle -Dtest.single=RunCukesTest < modulename > :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.

2) Add following code to your 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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