Run Single Test from a Junit Class Using Command-Line

Run single test from a JUnit class using command-line

You can make a custom, barebones JUnit runner fairly easily. Here's one that will run a single test method in the form com.package.TestClass#methodName:

import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;

public class SingleJUnitTestRunner {
public static void main(String... args) throws ClassNotFoundException {
String[] classAndMethod = args[0].split("#");
Request request = Request.method(Class.forName(classAndMethod[0]),
classAndMethod[1]);

Result result = new JUnitCore().run(request);
System.exit(result.wasSuccessful() ? 0 : 1);
}
}

You can invoke it like this:

> java -cp path/to/testclasses:path/to/junit-4.8.2.jar SingleJUnitTestRunner 
com.mycompany.product.MyTest#testB

After a quick look in the JUnit source I came to the same conclusion as you that JUnit does not support this natively. This has never been a problem for me since IDEs all have custom JUnit integrations that allow you to run the test method under the cursor, among other actions. I have never run JUnit tests from the command line directly; I have always let either the IDE or build tool (Ant, Maven) take care of it. Especially since the default CLI entry point (JUnitCore) doesn't produce any result output other than a non-zero exit code on test failure(s).

NOTE:
for JUnit version >= 4.9 you need hamcrest library in classpath

Compile and run test with JUnit in command line on a single Java class

After quite a lot of trial and error in the comments section, the root cause was an old JUnit 3.8.2 dependency. The 3.x releases used a different namespace that was changed in 4.x to org.junit.

Therefore the classes where not found while compiling the test.

To debug such issues unzipping the jar with unzip on Linux can be helpful.

Run a single test method with maven

To run a single test method in Maven, you need to provide the command as:

mvn test -Dtest=TestCircle#xyz test

where TestCircle is the test class name and xyz is the test method.

Wild card characters also work; both in the method name and class name.

If you're testing in a multi-module project, specify the module that the test is in with -pl <module-name>.

For integration tests use it.test=... option instead of test=...:

mvn -pl <module-name> -Dit.test=TestCircle#xyz integration-test

run a junit test for a single method only

I do not think if that is natively supported by JUnit. However, you have some alternatives:

  1. Put the required tests in different classes so you can run them separately
  2. Maybe using the @Ignore annotation helps you.
  3. If you're using some IDE, it may support this approach. For instance in Eclipse, unfold the test class in the Package Explorer, select the method you want to run, and click Run As -> JUnit test.
  4. Here's a quite extensive tutorial on how to create a custom TestSuit and an Ant task that can make this for you.
  5. UPDATE In this thread the guys say that "The old junit.textui.TestRunner utility provides a way to run a single method on the command-line, via the runSingleMethod(), but it doesn't support JUnit4 annotated test classes."

How to run a specific junit test in ant with parameter?

Introduce property which defines test name you want to execute:

<property name="unit.test" value="*.java" />

Use this property in your batchtest:

<batchtest fork="yes" todir="${output.test.dir}">
<fileset dir="${source.test.dir}" includes="**/${unit.test}"/>
</batchtest>

Pass value for this property to ant:

ant test -Dunit.test=A50Test

How to run only one unit test class using Gradle

To run a single test class Airborn's answer is good.

With using some command line options, which found here, you can simply do something like this.

gradle test --tests org.gradle.SomeTest.someSpecificFeature
gradle test --tests '*SomeTest.someSpecificFeature'
gradle test --tests '*SomeSpecificTest'
gradle test --tests 'all.in.specific.package*'
gradle test --tests '*IntegTest'
gradle test --tests '*IntegTest*ui*'
gradle test --tests '*IntegTest.singleMethod'
gradle someTestTask --tests '*UiTest' someOtherTestTask --tests '*WebTest*ui'

From version 1.10 of gradle it supports selecting tests, using a test filter. For example,

apply plugin: 'java'

test {
filter {
//specific test method
includeTestsMatching "org.gradle.SomeTest.someSpecificFeature"

//specific test method, use wildcard for packages
includeTestsMatching "*SomeTest.someSpecificFeature"

//specific test class
includeTestsMatching "org.gradle.SomeTest"

//specific test class, wildcard for packages
includeTestsMatching "*.SomeTest"

//all classes in package, recursively
includeTestsMatching "com.gradle.tooling.*"

//all integration tests, by naming convention
includeTestsMatching "*IntegTest"

//only ui tests from integration tests, by some naming convention
includeTestsMatching "*IntegTest*ui"
}
}

For multi-flavor environments (a common use-case for Android), check this answer, as the --tests argument will be unsupported and you'll get an error.

How to run a single method in a JUnit 4 test class?

This is a simple single method runner. It's based on JUnit 4 framework but can run any method, not necessarily annotated with @Test

    private Result runTest(final Class<?> testClazz, final String methodName)
throws InitializationError {
BlockJUnit4ClassRunner runner = new BlockJUnit4ClassRunner(testClazz) {
@Override
protected List<FrameworkMethod> computeTestMethods() {
try {
Method method = testClazz.getMethod(methodName);
return Arrays.asList(new FrameworkMethod(method));

} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
Result res = new Result();
runner.run(res);
return res;
}

class Result extends RunNotifier {
Failure failure;

@Override
public void fireTestFailure(Failure failure) {
this.failure = failure;
};

boolean isOK() {
return failure == null;
}

public Failure getFailure() {
return failure;
}
}


Related Topics



Leave a reply



Submit