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)?
Yes, We can pass command line arguments to a test execution by using -D JVM command-line options as shown below.
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.
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.
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