I would like to run JUnit test cases from the command line. How can I do this?
相关问题
- Dependencies while implementing Mocking in Junit4
- How to unit test a reactive component where ngCont
- org.apache.commons.fileupload.disk.DiskFileItem is
- softlinks atime and mtime modification
- Access for global variables JavaScript unit testin
相关文章
- How to replace file-access references for a module
- How to mock methods return object with deleted cop
- What is a good way of cleaning up after a unit tes
-
EF6 DbSet
returns null in Moq - React testing library: Test attribute / prop
- Setup and Tear Down of Complex Database State With
- React/JestJS/Enzyme: How to test for ref function?
- Compile and build with single command line Java (L
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:
Or run only 1 test-method myMethod from test-class CustomTest using next command:
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
If you project is ant based then you should be able to do something like this from the console:
If this doesn't work, but still your project is ant based, you can run
ant -p
to list the main targets of the project.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:
With JUnit 4.12 the following didn't work for me:
Apparently, from JUnit 4.11 onwards you should also include
hamcrest-core.jar
in your classpath:Maven way
If you use Maven, you can run the following command to run all your test cases:
Or you can run a particular test as below
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:
Gradle way
If you use Gradle, you can run the following command to run all your test cases:
Or you can run a particular test as below
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.
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):
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:
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):
Then run your test cases. For example:
Ensure that JUnit.jar is in your classpath, then invoke the command line runner from the console
Reference: junit FAQ