How to run external testcase(Class,junit) in java program?
+3
A:
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,...)
VonC
2009-07-29 05:38:53
classpath parameter should be -> -cp "1;2" or -cp "1:2"
h3xStream
2010-10-26 17:52:59
@h3xStream: you are right. I have edited and fixed the answer.
VonC
2010-10-26 18:03:36