Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

junit

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

like image 791
DogStory Avatar asked Jul 29 '09 03:07

DogStory


People also ask

How do I run a JUnit test from another class?

You could put that code in a utility method in a common class, and invoke the common code in both tests. use case: your app/framework supports several databases. so you have one base test class which contains tests for all features and one extension per database which creates the connection, loads the initial DDL/data.

How do I run JUnit test outside of Eclipse?

To start them in your IDE go to the test class, e.g. TestJunit , right-click, and select run as JUnit Test (or similar).

How do I run all JUnit test cases?

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


1 Answers

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,...)
like image 174
VonC Avatar answered Sep 22 '22 06:09

VonC