We have several junit test cases in a particular file, how do I run a certain test alone.
Note: I am aware of the @Ignore
annotation, but don't want to use it. Since you will still have to mark the rest of the use cases with the @Ignore
option.
You can specify the test you would like to run via the -Dtest=
parameter.
Just run it with this parameter values:
-Dtest=TestClass#singleTestCaseMethod
It is only supported in JUnit 4.x. More information: Running a single test
This depends on how you're running the tests.
If you're using Eclipse, you can either select the name of the method in the Java Editor and press F11/ctrl-F11 to Run As JUnit. This also works if the cursor is on the method name itself.
If you've already run the tests, and the JUnit view is open in Eclipse, you can rerun a particular method by right clicking on the test and selecting Run/Debug
. There will be similar functionality in Intellij.
If you're running from the command line, you can use JUnitCore.
java org.junit.runner.JUnitCore TestClass1 TestClass2
EDIT: For maven, you can specify the name of the method on the command line:
mvn -Dtest=TestCircle#mytest test
See the documentation for Maven Surefire - Running a Single Test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With