Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run all JUnit tests of a given package?

I use JUnit 4 in eclipse. I have some test classes in my package and want to run them all. How?

like image 913
snakile Avatar asked Jan 10 '10 10:01

snakile


4 Answers

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.

like image 159
John Avatar answered Nov 12 '22 10:11

John


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".

like image 33
Istvan Devai Avatar answered Sep 19 '22 00:09

Istvan Devai


Right-click on the package in the package explorer and select 'Run as' and 'Unit-Test'.

like image 23
DerMike Avatar answered Nov 12 '22 08:11

DerMike


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).

like image 7
user85421 Avatar answered Nov 12 '22 08:11

user85421