Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you run all JUnit tests in a package from the command line without explicitly listing them?

Provided the test classes and JUnit are both on the classpath, one can run JUnit tests from the command line as follows:

java org.junit.runner.JUnitCore TestClass1 TestClass2

Now, is there a way to run all tests in a package (and sub-packages) as well?

I'm looking for something like

java org.junit.runner.JUnitCore com.example.tests.testsIWantToRun.*

Is there an easy way of doing that (that doesn't involve maven or ant)?

like image 611
Christian Avatar asked Jul 01 '14 12:07

Christian


People also ask

Is it possible to pass command line arguments to a test execution in JUnit?

Yes, We can pass command line arguments to a test execution by using -D JVM command-line options as shown below.

What we have to type to run the file TestClass class from the command line?

To run the file TestClass. class from the command line, we have to type what? Explanation: The test cases are executed using JUnitCore class which is referenced by “org. junit.


1 Answers

Junit lets you define suites of tests. Each suite defines a collection of tests, and running the suite causes all of the tests to be run. What I do is to define a suite for each package, listing the test classes for that package along with the suites for any sub-packages:

package com.foo.bar;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import com.foo.bar.baz.Suite_baz;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    ThisTest.class,
    ThatTest.class,
    TheOtherTest.class,
    Suite_baz.class,
})
public class Suite_bar {
}

This isn't completely effortless. You have to construct the suites and manually update them with new test classes. I suppose it wouldn't be hard to write a little java program to generate these automatically, if someone wanted to.

like image 176
Kenster Avatar answered Sep 20 '22 23:09

Kenster