I use JUnit 4 in eclipse. I have some test classes in my package and want to run them all. How?
In eclipse if you right click the folder and select Run As JUnit Test only the tests in that folder will be run (i.e. tests in nested subfolders will not be run). In order to run all of the tests in a directory including tests in nested directories you will need to use something like googlecode.junittool box.
Using this I created something like the following
package com.mycompany.myproject.mymodule;
import org.junit.runner.RunWith;
import com.googlecode.junittoolbox.SuiteClasses;
import com.googlecode.junittoolbox.WildcardPatternSuite;
@RunWith(WildcardPatternSuite.class)
@SuiteClasses({ "**/*Test.class" })
public class RunAllMyModuleTests {
}
I added the required dependencies (jar files) using this in my mavin build (in addition to the junit dependency):
<dependency>
<groupId>com.googlecode.junit-toolbox</groupId>
<artifactId>junit-toolbox</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.8.2</version>
</dependency>
Right clicking on this class and selecting Run As JUnit test runs all of the tests in the specified directory including all tests in nested subfolders.
With JUnit5, you can easily create a "suite" class, that will run all tests in a package (or even subpackages, it works recursively):
@RunWith(JUnitPlatform.class)
@SelectPackages("my.test.package")
public class MySuite {
}
Once that's done, you can run this suite with "Run as Test".
Right-click on the package in the package explorer and select 'Run as' and 'Unit-Test'.
with JUnit 4 I like to use an annotated AllTests
class:
@RunWith(Suite.class)
@Suite.SuiteClasses({
// package1
Class1Test.class,
Class2test.class,
...
// package2
Class3Test.class,
Class4test.class,
...
})
public class AllTests {
// Junit tests
}
and, to be sure that we don't forget to add a TestCase to it, I have a coverage Test (also checks if every public method is being tested).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With