How to get gradle and cucumber working together?

2019-05-28 09:01发布

Getting gradle to work with cucumber cleanly is something of a challenge. I want to get gradle build to compile and run the tests, but so far I've had no success.

build.gradle

plugins {
   id "com.github.samueltbrown.cucumber" version "0.9"
}
apply plugin: 'java'
apply plugin: 'idea'


def JAVA_WEBSOCKET_VERSION = '1.2.1'
def CUCUMBER_VERSION = '1.2.4'
jar {
    manifest {
        attributes 'Implementation-Title': 'Java-WebSocket',
                   'Implementation-Version': JAVA_WEBSOCKET_VERSION
    }
}

repositories {
    jcenter()
}

dependencies {
    testCompile "info.cukes:cucumber-java:$CUCUMBER_VERSION"
    testCompile "info.cukes:cucumber-junit:$CUCUMBER_VERSION"
    testCompile 'junit:junit:4.+'
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}

Currently I get many errors about the annotations (@Given, @Then, @After) that cucumber uses. What I want is to build the project cleanly without using JavaExec. Is this possible or is there a specific limitation to either gradle or cucumber that prevents this?

2条回答
混吃等死
2楼-- · 2019-05-28 09:38

Please use the below gradle cucumber plugin in your build.gradle file

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

dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' compile 'org.codehaus.groovy:groovy:2.4.7' cucumberCompile 'info.cukes:cucumber-groovy:1.2.2' }

Running gradle cucumber in the terminal will get you started

查看更多
唯我独甜
3楼-- · 2019-05-28 09:47
dependencies {

    testCompile 'info.cukes:cucumber-jvm:1+'
    testCompile 'info.cukes:cucumber-jvm-deps:1+'
    testCompile 'info.cukes:cucumber-java:1+'
    testCompile 'info.cukes:cucumber-junit:1+'
    testCompile 'info.cukes:cucumber-core:1+'

}

I created another function to execute test

test { 

    ignoreFailures = true


    // show standard out and standard error of the test JVM(s) on the console
    testLogging.showStandardStreams = true

    // set heap size for the test JVM(s)
    minHeapSize = "128m"
    maxHeapSize = "512m"

    // set JVM arguments for the test JVM(s)
    jvmArgs '-XX:MaxPermSize=256m'

    // listen to events in the test execution lifecycle
    beforeTest { descriptor ->
        logger.lifecycle("Running test: " + descriptor)
    }


    // explicitly include or exclude tests( Add Package directly)
    exclude "com/**/***/rest/junit**"
    exclude "com/**/***/db/junit**"

    reports.junitXml.enabled = false
    reports.html.enabled = false
}

now Call this function from command line for test execution

task "forceTest" { 
    dependsOn "clean", "cleanTest", "test"
}
查看更多
登录 后发表回答