Reputation: 9785
Working with cucumber-groovy-example
result:
/opt/gradle/gradle-2.7/bin/gradle clean cucumber uberJar
:clean
:compileJava
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes
:jar
:startScripts
:distTar
:distZip
:assemble
:cucumber
# language: en
Feature: Division
In order to avoid silly mistakes
Cashiers must be able to calculate a fraction
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
@important
Scenario: Regular numbers # calc/division.feature:7
Given I have entered 3 into the calculator # CalculatorSteps.groovy:31
And I have entered 2 into the calculator # CalculatorSteps.groovy:31
When I press divide # CalculatorSteps.groovy:39
Then the stored result should be 1.5 # CalculatorSteps.groovy:43
Scenario: More numbers # calc/division.feature:13
Given I have entered 6 into the calculator # CalculatorSteps.groovy:31
And I have entered 3 into the calculator # CalculatorSteps.groovy:31
When I press divide # CalculatorSteps.groovy:39
Then the stored result should be 2.0 # CalculatorSteps.groovy:43
2 Scenarios (2 passed )
8 Steps (8 passed )
0m0.186s
:uberJar
BUILD SUCCESSFUL
Total time: 19.643 secs
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.7/userguide/gradle_daemon.html
How can i run the same tests from within a Java file: e.g.
src/main/java/calc/TestRunner.java
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;
@RunWith(Cucumber.class)
@CucumberOptions(format = {"pretty"}, monochrome=true)
public class TestRunner {
public static void main(String[] args) {
System.out.println("dummy main java");
// I want to run the tests from within the jar file
}
}
so that when i do java -jar build/distributions/cucumber-jvm-groovy-example.jar
i should get the same result as when i use gradle to run the tests via command line
i used the following Java Class:
import org.junit.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.List;
import org.junit.runner.notification.Failure;
@RunWith(Cucumber.class)
@CucumberOptions(format = {"pretty"}, monochrome=true)
public class TestRunner {
public static void main(String[] args) {
System.out.println("dummy main java");
JUnitCore.main("cucumberTest.TestRunner");
}
}
but got the exception:
initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [cucumberTest.TestRunner]
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
at TestRunner.main(TestRunner.java:19)
Caused by: java.lang.ClassNotFoundException: cucumberTest.TestRunner
Which makes me to think that JUnitCore.main() needs a class name as argument
Upvotes: 0
Views: 3158
Reputation: 91
other way can be to create a task and include runner class for test
build.gradle-
task RunCukesTest(type: Test) << {
include "RunCukesTest.class"
}
your class -
@RunWith(Cucumber.class)
@CucumberOptions(dryRun = false, strict = true, features = "src/test/resources", glue
= "com.gradle.featuretests",monochrome = true)
public class RunCukesTest {
}
And then simply run the following command :-
gradle RunCukesTest
Upvotes: 0
Reputation: 2819
One way is to use the command line runner. Java class Runtime allows you to execute command line from within Java. The key is cucumber.api.cli.Main.
Something like this:
task cucumber() {
dependsOn assemble, compileTestJava
doLast {
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
}
}
}
Upvotes: 0