as we are upgrading to junit5 from junit4 for unit testing purpose, i couldn't able to find solution to creating a test suite in junit5 like in junit4 :
below is the our junit4 suite class :
#RunWith(Suite.class)
#SuiteClasses({
ClassA.class,
ClassB.class,
ClassC.class
} )
public class TestSuite {
}
The Code i tried after searching around is below :
#RunWith(JUnitPlatform.class)
#SelectClasses( {
OrderAnnotationAlphanumericExperiment.class,
orderAnnotationExperiment.class,
ParameterizedAnnotation.class
} )
public class TestSuite {
}
on searching for the solution, most of them were providing a solution of using the same test suite to run it using vintage api, but what am looking for is to create test suite in junit 5.
Few suggesting #ExtendsWith(SpringExtension.class), but there is also less documentation for it, couldn't able to find a solution to create a suite in junit 5
few blogs/question/sites i referred :
Create TestSuite in JUnit5 (Eclipse)
Are test suites considered deprecated in JUnit5?
https://howtodoinjava.com/junit5/junit5-test-suites-examples/
some one help me out to solve this problem.
It's actually pretty easy. The following will find all JUnit tests in directory foo.bar.tests as well as it's subdirectories:
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.SuiteDisplayName;
import org.junit.runner.RunWith;
#RunWith(JUnitPlatform.class)
#SuiteDisplayName("JUnit Platform Suite Demo")
#SelectPackages("foo.bar.test")
public class FullTestSuite
}
See Sec 4.4.4 of the User's Guide.
In Eclipse Photon I have a project based on Java 1.8 with multiple Junit 5 Unit Tests. The individual unit tests work well and now I'm looking to generate a Junit Test Suite.
Based on the JUnit5 User Guide I have created a AllTest.java file that has the following code:
#RunWith(JUnitPlatform.class)
#SuiteDisplayName("Test Suite")
#SelectPackages("com")
public class AllTests
{
}
When I execute these Java file I get the following error message:
Screen Shot of Error Message
I've also tried the #SelectClasses and have run into the same issue.
Has anyone else seen this issue or know how to resolve it?
#RunWith(JUnitPlatform.class) is a JUnit4 annotation, so while executing the test we have to select RunConfiguration and select Junit 4 is the drop down instead of Junit 5. This will run the tests.
I have to following project structure:
The directory src/test/java/ic/tests contains junit tests and the directory src/test/features/ic contains cucumber test (feature files).
But when I do a maven run (mvn test -Dcucumber.options="src/test/features/ic --tags #IC-115") to execute a single cucumber test the executor starts the junit tests in the src/test/java/ic/tests directory...
Only the corresponding feature file is annotated with #IC-115.
Even the absolute version mvn test -Dcucumber.options="C:\Users_Clemens_\Documents\test-ic\src\test\resources\features\ic\IC-115-LogOut.feature" does not execute my test.
How can I execute the single cucumber test that I want to execute?
Try to run the command with the name of the feature (exact way to the feature).
mvn test -Dcucumber.options="src/test/features/ic/FeatureName.feature"
Or if the feature is composed by more than one test you could set a specific(not used for others scenarios) tag to the test and run with
mvn verify -Dcucumber.options="--tags #specifictag"
Could solve it by adding a runner class AND moved feature files to "src/test/resources" AND added the maven-surefire-plugin with adding an exclusion to the runner class. Seems like these 3 steps are all necessary.
package kiwigrid;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features="src/test/resources")
public class Runner {
}
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.
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.