Running Single cucumber-jvm .feature file from Gradle - java

I am integrating a cucumber-java with an existing gradle java project, that has a focus on test automation. There is no production code within this project, so making the entire project makes little sense.
What I would like to do is to create gradle tasks or a single gradle task with a -D property that specifies a cucumber .feature file to run cucumber-jvm on. All of the examples that I've seen show how to get cucumber-jvm to run as part of the build process. Instead, I would like to define a task testCucumber(type: Test) to run a single .feature file. How would I go about doing this?

I was able to get this to work by using a javaexec task in gradle.
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'junit:target/cucumber-junit-report.xml', '--plugin', 'pretty', '--glue', 'StepDefinitions', 'src/test/Features/FeatureFilename.feature']
}
This is assuming that all step definitions are defined within a package called "StepDefinitions".

Related

How to run main file using gradle?

I have an application in java written in IntelliJ using gradle. Right now I can run tests but I'd like to run main file to check execution by myself. Does anyone know how to run main file?
Looks like you use Java. In this case:
Apply application plugin
Define mainClassName property
The build.gradle.kts fill be like:
plugins {
application
}
application {
mainClassName = "org.gradle.sample.Main"
}
Next you can start application via gradlew run

How standard settings suppose to be in build.gradle if use Cucumber JVM and Allure?

I'm able to run Cucumber JVM with Gradle but not sure what exactly I need to add in build.gradle to get Allure report after test execution.
I checked an official website and read section Cucumber JVM but there is information only about maven project [cucumber jvm|https://docs.qameta.io/allure/#_cucumber_jvm].
Could anybody give an example with the appropriate setting for build.gradle file if we use the combination of Cucumber JVM + Gradle + Allure.
Finally found the solution.
Add task Cucumber as recommended here (https://cucumber.io/docs/tools/java/#gradle)
Add the following in args for task cucumber
args = [ '--plugin', 'io.qameta.allure.cucumber4jvm.AllureCucumber4Jvm', '--glue', 'path_to_your_glue_code', ("path_to_your_feature")]
Add dependency for the plugin
dependencies { testCompile("io.qameta.allure:allure-cucumber4-jvm:2.10.0") }
Add allure plugin (https://plugins.gradle.org/plugin/io.qameta.allure)
From the command line gradle cucumber and allure-reports directory will be generated in your root project.
The final step to generate html report
allure generate 'path_to_allure_results_folder' -c -o 'path_to_your_report_folder\'

Execute Groovy script from Gradle without compiling Java

I have a Java project with Gradle.
Also I use Groovy to generate some class that will be used in Java code. Gradle executes script in separate task below:
task generateClass(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'generatorScript'
}
If I run this task, it first starts Java compilation, and only after that executes script. So if compilation failed, my generator script will not be executed. As it was mentioned, script generates one class, which my Java code is actually depends on, so, if not generated, Java will not be compiled. Vicious circle.
Script itself does not depend on some Java classes and is placed in separate sources directory:
/src
/main
/java
/...(java classes)
/groovy
generatorScript.groovy
It seems like nothing interferes me to execute script separately and independently from Java compilation.
How can I achieve that?
The problem is that you have the generator groovy script in the main sourcesets, and you try to compile this groovy script to use it as classpath for your JavaExec task. This is why compileJava task is executed, I guess.
You could do in another way, using groovy.ui.GroovyMain to execute your script, with following solution based on this link
configurations {
// a dedicated Configuration for Groovy classpath
groovyScript
}
dependencies {
// Set Groovy dependency so groovy.ui.GroovyMain can be found.
groovyScript localGroovy()
}
task generateClass(type: JavaExec) {
// Set class path used for running Groovy command line.
classpath = configurations.groovyScript
// Main class that runs the Groovy command line.
main = 'groovy.ui.GroovyMain'
// Pass your groovy script as argument of the GroovyMain main
// (can be improved)
args 'src/main/groovy/generatorScript.groovy'
}

How to make Cucumber features run in an Android project as local unit tests?

I have an Android project written in Java that I'm working on in Android Studio.
I'd like to use Cucumber for integration testing of some internal components (note: I know this is not the BDD way, nonetheless useful to me). I want the tests to run as local unit tests (without Instrumentation) using gradlew test because the components under test do not interact with the Android SDK.
My problem is that the Cucumber features are not recognized by Gradle and do not run when I run gradlew test.
Here's how I set it up so far:
Added these dependencies to my app's build.gradle:
testImplementation 'io.cucumber:cucumber-java:3.0.2'
testImplementation 'io.cucumber:cucumber-junit:3.0.2'
testImplementation 'io.cucumber:cucumber-jvm:3.0.2'
Also there, I added the path to where I've put my Feature file:
android {
...
sourceSets {
test {
assets.srcDirs = ['src/test/java/integrationTest/assets']
}
}
}
This is based on this folder structure:
Added a class for the steps (Steps1.java) as can be seen above.
What am I missing here?
Your feature files are probably not getting picked up because you did not include a runner. You can either create a JUnit Runner or use the Gradle cucumber plugin. I am not sure if either would work in Android though.
Also you don't need io.cucumber:cucumber-jvm:3.0.2 as a dependency. It is only a pom.
I've found out how to configure my Android app to run my Cucumber tests as "local unit tests" when I run gradlew test from the command line. This is based on my SO question here and I've written a "HOWTO" blog post about it here: Android: Run Cucumber tests without a device or an emulator.
The key to getting it done is in the app's build.gradle file. By latching on to the unitTests.all hook in the testOptions section I am able to run Cucumber using javaexec like so:
android {
...
testOptions {
unitTests.all {
def classpath2 = getClasspath()
javaexec {
main = "cucumber.api.cli.Main"
classpath = classpath2
args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/java/cucumber/assets']
}
}
}
)

Gradle - running a junit test not finding my xml file

I'm having a simple junit test task.
task testSanity(type: Test, dependsOn: testClasses) {
def springInstrumentJarPath = project.configurations.compile.find { it.name.startsWith("spring- instrument") }
jvmArgs '-javaagent:' + springInstrumentJarPath
exclude '**/*TimeBaseTest*'
exclude '**/*SuiteTests*'
}
Problem is i added a new xml file for my ehcache at:
/src/main/webapp/WEB-INF/classes/ehcache.xml
And all my test fails since junit is not able to find the file.
How can I define this in the gradle task?
Thanks.
src/main/webapp should only contain files that aren't used from Java code (more precisely, files that aren't loaded by a class loader). Java resources should instead go into src/main/resources. Hence, moving the file to src/main/resources/ehcache.xml should solve the problem.

Categories