Home >Java >javaTutorial >How to Run JUnit Test Cases from the Command Line?
Executing JUnit Test Cases via Command Line
Performing JUnit test execution from the command line provides an efficient means of automating the testing process. To achieve this, you can utilize various approaches depending on the version of JUnit employed.
JUnit 5.x
For JUnit 5.x, the preferred method is:
java -jar junit-platform-console-standalone-<version>.jar <Options>
Refer to https://stackoverflow.com/a/52373592/1431016 for a concise overview and https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher for comprehensive details.
JUnit 4.X
When using JUnit 4.X, execute tests with:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
JUnit 3.X
Note that for JUnit 3.X, the class name differs:
java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name]
Additional Considerations
Depending on your setup, you may need to modify the classpath and include additional JARs or class file directories. Separate these paths using semicolons (Windows) or colons (UNIX/Linux).
Also, Java 6 and above supports globs in the classpath, enabling you to use commands like:
java -cp lib/*.jar:/usr/share/java/junit.jar ...
Remember, writing tests is crucial for maintaining code quality, and leveraging the command line provides a convenient way to execute unit tests with ease.
The above is the detailed content of How to Run JUnit Test Cases from the Command Line?. For more information, please follow other related articles on the PHP Chinese website!