JVM Cucumber Execution - java

I am building a framework that is able to execute Selenium instructions and Cucumber instructions. I have created the runner for Cucumber in a seperate empty class with the following code
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
public class RunCukesTest {
}
I also will have a processor for Selenium instructions in a separate class.
prop.load(ClassLoader.getSystemResourceAsStream("src/main/resources/runConfig.properties"));
if(prop.getProperty("instructionsheet").contains("InstructionSheets")){
KeywordProcessor kp = new KeywordProcessor();
}else if(prop.getProperty("instructionsheet").contains("FeatureFiles")){
RunCukesTest runCukes = new RunCukesTest();
}
How can I execute the Cucumber runner class if it's an empty class?, do I just create an instance of the runner class for cucumber? or do I have to spacify something else

#RunWith(Cucumber.class) is used to integrate cucumber with the existing infrastructure for running JUnit tests. If you start to roll out your own framework, it is worth to look other places. cucumber.api.cli.Main could be a good place to start your journey.

The class RunCukesTest is a class that will be executed by the JUnit runner Cucumber.class since it is annotated with #RunWith(Cucumber.class).
In other words, it will be executed when you run the tests the same way as any other JUnit class.
The class is empty, and has to be empty, to force a separation of the execution of Cucumber and the steps supporting the execution.
If you want to execute Cucumber from another context, say your own framework, look at the command line interface. You are able to run its main() from any Java class, just call the static method and wait for the result.

Related

Run cucumber tests in a serial fashion in Java

I have a number of cucumber .feature file written in Java.
It looks like they are running in parallel.
How do I run the cucumber tests one by one in a single threaded fashion?
The run test config is:
#RunWith(Cucumber.class)
#CucumberOptions(strict = true,
glue = "com.myproject.steps",
features = "classpath:cucumber/features/",
format = { "pretty", "html:target/report/cucumber",
"json:target/report/cucumber/cucumber.json" })
public class RunIntegrationTest {
}
You are using Junit and junit not have such parallel execution functionality
Another thing is cucumber cucumber-jvm-parallel, are you using it?
Does you have multiple TestRunner java file with CucumberOptions in src.test.java package, if so keep one class file only

How can I run a single Serenity test runner class (among several) in Gradle?

I'm using Serenity + JBehave in Java, run by Gradle. I have several test runner classes pointing at different sets of stories. I need to be able to specify which one I run. One runner file is below to give an example of my implementation there.
import net.serenitybdd.jbehave.SerenityStories;
public class Debug extends SerenityStories {
public Debug() {
findStoriesIn("**/ldap");
}
}
You can do following in build.gradle, so when ever you run
task runSpecificRunner(type: Test) {
include '**/**YOURRunnerName.class'
}
Then run ./gradlew runSpecificRunner aggregate, you will see only that specific runner will be executed.

Finding all tests in a subpackage

When I try running JUnit tests, I get the error message
No tests found with test runner 'JUnit 4'
and therefore I have tried solutions from
No tests found with test runner 'JUnit 4'
junit: no tests found
'No JUnit tests found' in Eclipse
However, in my case, the difference seems to be that I specify the tests to run on package level instead of folder level.
Running JUnit tests works if I specify a package that directly contains test classes (e.g. com.example.tests.smoketests) but does not work if a higher level package is specified (e.g. com.example.tests).
If I define a test in com.example.tests, it is found and run.
Is there a way to let Eclipse/JUnit find tests in a package and all subpackages?
Out of the box, there is nothing to do this for you. You can use the Suite annotation to achieve this goal in JUnit4, though this still requires you to manually define a Suite test for each package, and then include all of them in a aggregate Suite test (such they recursively call child Suites).
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import a.sub.package.AnotherSuiteTest.class;
#RunWith(Suite.class)
#Suite.SuiteClasses({
PackageLocalTestClass1.class,
PackageLocalTestClass2.class,
AnotherSuiteTest.class
})
public class JunitTestSuite {
}
I've played around with building my own utility class before which creates the entire series of tests. This article provides an analogous utility.

Run all tests in Junit 4

I want to be able to run all tests in a project programmatically. I know Eclipse has a "Run as JUnit test" configuration which somehow grabs all the tests in a project and run them. Is there any way for me to also grab the list of tests programmatically and run them? Or is there some good way to construct a test suite containing all the test cases without manually listing out every one (all 700+) of them?
I've tried the "New... -> Test Suite" option in Eclipse, but that seems to work only for JUnit 3, identifying tests by their extending from TestCase
The test classes are JUnit 4, so their only distinguishing characteristic is the annotation, no naming convention, no subclassing from TestCase.
Thanks in advance!
Though it does not really solve your immediate problem, I find it a very useful general practice to create suites and suites of suites, e.g. for a package something like PackageFooSuite etc. and assemble these suites in one or more suites again, like ModuleFooSuite and have one top-level suite, like AllTestsSuite. That way it's easy to run both all tests in one step as well as submodule tests for the package I'm currently working on (and have the tests run quicker than if I would always run all of them):
#RunWith(Suite.class)
#Suite.SuiteClasses({ PackageFooSuite.class, PackageBarSuite.class} )
public final class AllTestsSuite {} // or ModuleFooSuite, and that in AllTests
None of the other answers did it for me. I had 40k tests I needed to run, so manually listing every class was not an option.
I did it with ClasspathSuite. A test suite that runs all Junit4 and Junit3 test cases in the class path is as follows:
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.*;
import org.junit.runner.RunWith;
import org.junit.runner.JUnitCore;
import static org.junit.extensions.cpsuite.SuiteType.*;
#RunWith(ClasspathSuite.class)
#SuiteTypes({ JUNIT38_TEST_CLASSES, TEST_CLASSES })
public class RunAllSuite {
/* main method not needed, but I use it to run the tests */
public static void main(String args[]) {
JUnitCore.runClasses(RunAllSuite.class);
}
}
I needed to run it from command line, so this is what I did:
Downloaded cp-1.2.6.jar
Create the previously mentioned RunAllSuite
Compile the class, javac RunAllSuite.java -cp cpsuite-1.2.6.jar;junit-4.8.1.jar
run it with target tests in the class path, java -cp cpsuite-1.2.6.jar;junit-4.8.1.jar;path/to/runallsuite/folder;target/classes;target/test-classes RunAllSuite
And that's it. With the RunAllSuite above, anywhere in your code you can just do JUnitCore.runClasses(RunAllSuite.class), which runs all tests in class path. There are other config options as well which are explained in the ClasspathSuite home page.
Note also that the class given above does not print anything. If that is needed, you can do
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.*;
import org.junit.runner.RunWith;
import org.junit.runner.JUnitCore;
import org.junit.internal.TextListener;
import static org.junit.extensions.cpsuite.SuiteType.*;
#RunWith(ClasspathSuite.class)
#SuiteTypes({ JUNIT38_TEST_CLASSES, TEST_CLASSES })
public class RunAllSuite {
public static void main(String args[]) {
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(RunAllSuite.class);
}
}
You can do this fairly easily from within maven using the surefire plugin: I usually clean/compile/install my projects from the command line before comparing them for eclipse usage (mvn eclipse:clean eclipse:eclipse) and you can define a test suite in your pom which lists all the tests you want to run en masse every time you run mvn install. You're not calling them programatically, exactly, but you can certainly call them en masse.
In Eclipse (I'm using 4.6.1) - Right click the project folder, select "Run As", choose "JUnit Test"
It will run all tests in that project. Same for a package.
Of the top of my head using Spring:
Implement a TypeFilter that matches classes with methods annotated with #Test (don't forget to consider the superclasses)
Invoke classpath scanning on your top-most test package
Invoke the JUnitRunner with the scan results
More info on classpath scanning and custom type filters here
With Eclipse Indigo (possibly Helios as well) in the Run Configurations dialog box, you now have the ability to Run all tests in a selected project, package or source folder.
Also a good reference from Eclipse is the article Java Unit testing with JUnit 4.x in Eclipse.
I also recommend using the JUnit Suite annotations. Follow the link for more detail.

Running a JUnit test from Groovy Console

How can I use the Groovy Console to kick off junit tests?
(Currently using Groovy 1.6.0)
I currently use:
junit.textui.TestRunner.run(MyTest)
(Where my junit test class is MyTest)
I've tried running:
MyTest
within the Groovy Console, but that just returns the class instance. Is there an easier way?

Categories