DogStory
DogStory

Reputation: 93

How to run external testcase(Class,junit) in java program?

How to run external testcase(Class,junit) in java program?

Upvotes: 7

Views: 9739

Answers (1)

VonC
VonC

Reputation: 1323553

If you want to run JUnit tests through a Java program, you could use the JUnitCore class

JUnitCore is a facade for running tests.
It supports running JUnit 4 tests, JUnit 3.8.x tests, and mixtures.
To run tests from the command line, run:

(windows)

java -cp /path/to/junit.jar;/path/to/yourTextClasses org.junit.runner.JUnitCore TestClass1 TestClass2 .... 

(Unix)

java -cp /path/to/junit.jar:/path/to/yourTextClasses org.junit.runner.JUnitCore TestClass1 TestClass2 .... 

For one-shot test runs, use the static method runClasses(Class[]).

Make sure you have junit.jar in your classpath, and the jar or classes of your external tests also in the the classpath.

That way you can execute them from the command-line (which may not be what you are after) or directly within your java program.

JUnitCore.runClasses(TestClass1,TestClass2,...)

Upvotes: 9

Related Questions