Permalink
Browse files

add command line parameter to run single method.

  • Loading branch information...
1 parent daeda1a commit d1f506e0915874a0388af3f87c791a95834f8d1b @schaarsc committed Jul 31, 2012
@@ -8,6 +8,7 @@
import org.junit.internal.RealSystem;
import org.junit.internal.TextListener;
import org.junit.internal.runners.JUnit38ClassRunner;
+import org.junit.runner.manipulation.Filter;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
@@ -26,6 +27,11 @@
* @since 4.0
*/
public class JUnitCore {
+ /**
+ * command line paramter used to run single methods instead of complete test classes.
+ */
+ public final static String METHOD = "--method=";
+
private final RunNotifier fNotifier= new RunNotifier();
/**
@@ -77,11 +83,19 @@ public static Result runClasses(Class<?>... classes) {
*/
private Result runMain(JUnitSystem system, String... args) {
system.out().println("JUnit version " + Version.id());
+ List<String> methods = new ArrayList<String>();
List<Class<?>> classes= new ArrayList<Class<?>>();
List<Failure> missingClasses= new ArrayList<Failure>();
for (String each : args)
try {
- classes.add(Class.forName(each));
+ if(each.startsWith(METHOD)) {
+ String[] m = each.split("=");
+ if(m.length==2 && !m[1].isEmpty()) {
+ methods.add(m[1]);
+ }
+ } else {
+ classes.add(Class.forName(each));
+ }
} catch (ClassNotFoundException e) {
system.out().println("Could not find class: " + each);
Description description= Description.createSuiteDescription(each);
@@ -90,7 +104,12 @@ private Result runMain(JUnitSystem system, String... args) {
}
RunListener listener= new TextListener(system);
addListener(listener);
- Result result= run(classes.toArray(new Class[0]));
+ Result result= null;
+ if (methods.size()>0) {
+ result = run(createMethodFilter(methods), classes.toArray(new Class[0]));
+ } else {
+ result = run(classes.toArray(new Class[0]));
+ }
for (Failure each : missingClasses)
result.getFailures().add(each);
return result;
@@ -104,6 +123,44 @@ public String getVersion() {
}
/**
+ * Construct new {@link Filter} based on method list.
+ * @param methods
+ * @return filter to run only methods listed in <code>methods</code>
+ */
+ private Filter createMethodFilter(final List<String> methods) {
+ return new Filter() {
+ @Override
+ public boolean shouldRun(Description description) {
+ String methodName = description.getMethodName();
+ if (methodName == null) {
+ return true;
+ } else if (methods.contains(methodName)) {
+ return true;
+ }
+ return false;
+ }
+ @Override
+ public String describe() {
+ return "Filter method";
+ }
+ };
+ }
+
+ /**
+ * Run only test matching filter.
+ * @param filter applied to <code>classes</code> before run
+ * @param classes the classes containing tests
+ * @return a {@link Result} describing the details of the test run and the failed tests.
+ */
+ public Result run(Filter filter, Class<?>... classes) {
+ Request request = Request.classes(defaultComputer(), classes);
+ if (filter!=null) {
+ request = request.filterWith(filter);
+ }
+ return run(request);
+ }
+
+ /**
* Run all the tests in <code>classes</code>.
* @param classes the classes containing tests
* @return a {@link Result} describing the details of the test run and the failed tests.
@@ -1,6 +1,7 @@
package org.junit.tests.running.core;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
@@ -15,6 +16,7 @@
private ByteArrayOutputStream results;
private PrintStream oldOut;
private static boolean testWasRun;
+ private static boolean test2WasRun;
@Before public void before() {
oldOut= System.out;
@@ -30,6 +32,9 @@
@Test public void test() {
testWasRun= true;
}
+ @Test public void test2() {
+ test2WasRun= true;
+ }
}
@Test public void runATest() {
@@ -41,7 +46,41 @@ public void run() {
});
assertTrue(testWasRun);
}
-
+
+ @Test public void runSingleMethod() {
+ testWasRun = false;
+ new MainRunner().runWithCheckForSystemExit(new Runnable() {
+ public void run() {
+ JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example", JUnitCore.METHOD+"test");
+ }
+ });
+ assertTrue(testWasRun);
+ }
+
+ @Test public void runTwoMethods() {
+ testWasRun = false;
+ test2WasRun = false;
+ new MainRunner().runWithCheckForSystemExit(new Runnable() {
+ public void run() {
+ JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example", JUnitCore.METHOD+"test", JUnitCore.METHOD+"test2");
+ }
+ });
+ assertTrue(testWasRun);
+ assertTrue(test2WasRun);
+ }
+
+ @Test public void donotRunSingleMethod() {
+ testWasRun = false;
+ test2WasRun = false;
+ new MainRunner().runWithCheckForSystemExit(new Runnable() {
+ public void run() {
+ JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example", JUnitCore.METHOD+"tst");
+ }
+ });
+ assertFalse(testWasRun);
+ assertFalse(test2WasRun);
+ }
+
@Test public void runAClass() {
testWasRun= false;
JUnitCore.runClasses(Example.class);

0 comments on commit d1f506e

Please sign in to comment.