KK.
KK.

Reputation: 4197

How to run JUnit test cases from the command line

I would like to run JUnit test cases from the command line. How can I do this?

Upvotes: 402

Views: 538307

Answers (12)

user unknown
user unknown

Reputation: 36259

JUnit5, no build-tools, no IDE

You invoke the the standalone-console version, given it's path is JUNITPATH - wether absolute or relative might depend on your requirements and taste, but it has to fit, of course - like this (adjust the version number):

To run a specific TESTCLASS only (note: no .class extension)

java -jar JUNITPATH/junit-platform-console-standalone-1.12.0.jar \
    execute \
    -cp PATH_TO_YOUR_TEST:PATH_TO_YOUR_CLASSES \
    -c YOUR_TESTCLASS

Note, that the command execute is sensitive to ordering; it has to appear before the options.

To run ALL tests in a given Classpath

java -jar JUNITPATH/junit-platform-console-standalone-1.12.0.jar \
    -cp PATH_TO_YOUR_TESTS:PATH_TO_YOUR_CLASSES \
    --scan-classpath`

There are plenty of options to select which tests to run (i.e.: files, directories, packages, modules), which are (interestingly/annoyingly) displayed, if your command fails, but not if you call the jar with --help.

The long path names make it convenient to use an alias (on unixoid systems) for your command, a shell function, symbolic links to jar file and so on.

There are plenty of subtle points, where your program may fail, so be sure

  • to use ";" as a path separator on Win and ":" on Linux/Mac/Solaris/... .
  • Don't forget to include "." for the current directory if you run tests from the current directory
  • Use fully qualified package names for package, if they aren't in the anonymous package (no package declaration) For the class in the -c CLASS - variant.

Maybe you start with the `junit-platform-console-standalone-x.y.z.jar, the class under test and the TestClass all in the current directory:

java -jar junit-platform-console-standalone-1.12.0.jar \
        execute -cp . -c XyTest 

You may omit the keyword execute from the test (for version 1.12.0), but then you get a warning:

WARNING: Delegated to the 'execute' command.
         This behaviour has been deprecated and will be removed in a future release.
         Please use the 'execute' command directly.

which took me quite a time, to figure out, that it has to be written BEFORE the -cp option.

Upvotes: 0

hel
hel

Reputation: 513

I had the same requirement to run a Test Class (test cases) I created in IntelliJ, using command line.

The following steps worked for me.

• Download “JUnit Standalone Launcher” JAR file from the below link.

https://junit.org/junit5/docs/5.0.0-M5/user-guide/#running-tests-console-launcher-options

• Change into the IntelliJ project's root directory.

cd <root_directory>

• Put the downloaded JAR file in the IntelliJ Project’s root directory.

• Create a new directory named “target” in the root directory.

mkdir target

• First, compile the Java Source Files and put them in the “target” folder. Use the below command.

javac -d target src/main/java/org/example/SourceFile.java

• Then, compile the JUnit Test Files and put them in the “target” folder.

javac -d target -cp target:junit-platform-console-standalone-1.9.3.jar src/test/java/org/example/TestSourceFile.java

• Finally, run the JUnit Test Cases.

java -jar junit-platform-console-standalone-1.9.3.jar --class-path target --select-class org.example.TestSourceFile

Thank you.

Upvotes: 2

lzap
lzap

Reputation: 17173

For JUnit 5.x it's:

java -jar junit-platform-console-standalone-<version>.jar <Options>

Find a brief summary at https://stackoverflow.com/a/52373592/1431016 and full details at https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher

For JUnit 4.X it's really:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

But if you are using JUnit 3.X note the class name is different:

java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name]

You might need to add more JARs or directories with your class files to the classpath and separate that with semicolons (Windows) or colons (UNIX/Linux). It depends on your environment.

Edit: I've added current directory as an example. Depends on your environment and how you build your application (can be bin/ or build/ or even my_application.jar etc). Note Java 6+ does support globs in classpath, you can do:

java -cp lib/*.jar:/usr/share/java/junit.jar ...

Write tests! :-)

Upvotes: 309

Yuci
Yuci

Reputation: 30189

Maven way

If you use Maven, you can run the following command to run all your test cases:

mvn clean test

Or you can run a particular test as below

mvn clean test -Dtest=your.package.TestClassName
mvn clean test -Dtest=your.package.TestClassName#particularMethod

If you would like to see the stack trace (if any) in the console instead of report files in the target\surefire-reports folder, set the user property surefire.useFile to false. For example:

mvn clean test -Dtest=your.package.TestClassName -Dsurefire.useFile=false

Gradle way

If you use Gradle, you can run the following command to run all your test cases:

gradle test

Or you can run a particular test as below

gradle test --tests your.package.TestClassName
gradle test --tests your.package.TestClassName.particularMethod

If you would like more information, you can consider options such as --stacktrace, or --info, or --debug.

For example, when you run Gradle with the info logging level --info, it will show you the result of each test while they are running. If there is any exception, it will show you the stack trace, pointing out what the problem is.

gradle test --info

If you would like to see the overall test results, you can open the report in the browser, for example (Open it using Google Chrome in Ubuntu):

google-chrome build/reports/tests/index.html

Ant way

Once you set up your Ant build file build.xml, you can run your JUnit test cases from the command line as below:

ant -f build.xml <Your JUnit test target name>

You can follow the link below to read more about how to configure JUnit tests in the Ant build file: https://ant.apache.org/manual/Tasks/junit.html

Normal way

If you do not use Maven, or Gradle or Ant, you can follow the following way:

First of all, you need to compile your test cases. For example (in Linux):

javac -d /absolute/path/for/compiled/classes -cp /absolute/path/to/junit-4.12.jar /absolute/path/to/TestClassName.java

Then run your test cases. For example:

java -cp /absolute/path/for/compiled/classes:/absolute/path/to/junit-4.12.jar:/absolute/path/to/hamcrest-core-1.3.jar org.junit.runner.JUnitCore your.package.TestClassName

Upvotes: 170

djangofan
djangofan

Reputation: 29689

Personally I would use the Maven surefire JUnit runner to do that.

Upvotes: 4

Stefan van den Akker
Stefan van den Akker

Reputation: 7009

With JUnit 4.12 the following didn't work for me:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

Apparently, from JUnit 4.11 onwards you should also include hamcrest-core.jar in your classpath:

java -cp .:/usr/share/java/junit.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore [test class name]

Upvotes: 20

Raghu K Nair
Raghu K Nair

Reputation: 3942

Alternatively you can use the following methods in JunitCore class http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html

run (with Request , Class classes and Runner) or runClasses from your java file.

Upvotes: 0

user1092126
user1092126

Reputation:

If your project is Maven-based you can run all test-methods from test-class CustomTest which belongs to module 'my-module' using next command:

mvn clean test -pl :my-module -Dtest=CustomTest

Or run only 1 test-method myMethod from test-class CustomTest using next command:

mvn clean test -pl :my-module -Dtest=CustomTest#myMethod

For this ability you need Maven Surefire Plugin v.2.7.3+ and Junit 4. More details is here: http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

Upvotes: 6

rogue lad
rogue lad

Reputation: 2452

In windows it is

java -cp .;/path/junit.jar org.junit.runner.JUnitCore TestClass [test class name without .class extension]

for example: c:\>java -cp .;f:/libraries/junit-4.8.2 org.junit.runner.JUnitCore TestSample1 TestSample2 ... and so on, if one has more than one test classes.

-cp stands for class path and the dot (.) represents the existing classpath while semi colon (;) appends the additional given jar to the classpath , as in above example junit-4.8.2 is now available in classpath to execute JUnitCore class that here we have used to execute our test classes.

Above command line statement helps you to execute junit (version 4+) tests from command prompt(i-e MSDos).

Note: JUnitCore is a facade to execute junit tests, this facade is included in 4+ versions of junit.

Upvotes: 12

Indraneel
Indraneel

Reputation: 316

Actually you can also make the Junit test a runnable Jar and call the runnable jar as java -jar

Upvotes: 5

rand_acs
rand_acs

Reputation: 725

The answer that @lzap gave is a good solution. However, I would like to add that you should add . to the class path, so that your current directory is not left out, resulting in your own classes to be left out. This has happened to me on some platforms. So an updated version for JUnit 4.x would be:

java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]

Upvotes: 54

Gishu
Gishu

Reputation: 136663

Ensure that JUnit.jar is in your classpath, then invoke the command line runner from the console

java org.junit.runner.JUnitCore [test class name]

Reference: junit FAQ

Upvotes: 24

Related Questions