Skip to content

Commit

Permalink
add command line parameter to run single method.
Browse files Browse the repository at this point in the history
  • Loading branch information
schaarsc committed Jul 31, 2012
1 parent daeda1a commit d1f506e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
61 changes: 59 additions & 2 deletions src/main/java/org/junit/runner/JUnitCore.java
Expand Up @@ -8,6 +8,7 @@
import org.junit.internal.RealSystem; import org.junit.internal.RealSystem;
import org.junit.internal.TextListener; import org.junit.internal.TextListener;
import org.junit.internal.runners.JUnit38ClassRunner; import org.junit.internal.runners.JUnit38ClassRunner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.notification.Failure; import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener; import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier; import org.junit.runner.notification.RunNotifier;
Expand All @@ -26,6 +27,11 @@
* @since 4.0 * @since 4.0
*/ */
public class JUnitCore { 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(); private final RunNotifier fNotifier= new RunNotifier();


/** /**
Expand Down Expand Up @@ -77,11 +83,19 @@ public static Result runClasses(Class<?>... classes) {
*/ */
private Result runMain(JUnitSystem system, String... args) { private Result runMain(JUnitSystem system, String... args) {
system.out().println("JUnit version " + Version.id()); system.out().println("JUnit version " + Version.id());
List<String> methods = new ArrayList<String>();
List<Class<?>> classes= new ArrayList<Class<?>>(); List<Class<?>> classes= new ArrayList<Class<?>>();
List<Failure> missingClasses= new ArrayList<Failure>(); List<Failure> missingClasses= new ArrayList<Failure>();
for (String each : args) for (String each : args)
try { 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) { } catch (ClassNotFoundException e) {
system.out().println("Could not find class: " + each); system.out().println("Could not find class: " + each);
Description description= Description.createSuiteDescription(each); Description description= Description.createSuiteDescription(each);
Expand All @@ -90,7 +104,12 @@ private Result runMain(JUnitSystem system, String... args) {
} }
RunListener listener= new TextListener(system); RunListener listener= new TextListener(system);
addListener(listener); 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) for (Failure each : missingClasses)
result.getFailures().add(each); result.getFailures().add(each);
return result; return result;
Expand All @@ -103,6 +122,44 @@ public String getVersion() {
return Version.id(); return Version.id();
} }


/**
* 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>. * Run all the tests in <code>classes</code>.
* @param classes the classes containing tests * @param classes the classes containing tests
Expand Down
41 changes: 40 additions & 1 deletion src/test/java/org/junit/tests/running/core/CommandLineTest.java
@@ -1,6 +1,7 @@
package org.junit.tests.running.core; package org.junit.tests.running.core;


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;


import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
Expand All @@ -15,6 +16,7 @@ public class CommandLineTest {
private ByteArrayOutputStream results; private ByteArrayOutputStream results;
private PrintStream oldOut; private PrintStream oldOut;
private static boolean testWasRun; private static boolean testWasRun;
private static boolean test2WasRun;


@Before public void before() { @Before public void before() {
oldOut= System.out; oldOut= System.out;
Expand All @@ -30,6 +32,9 @@ static public class Example {
@Test public void test() { @Test public void test() {
testWasRun= true; testWasRun= true;
} }
@Test public void test2() {
test2WasRun= true;
}
} }


@Test public void runATest() { @Test public void runATest() {
Expand All @@ -41,7 +46,41 @@ public void run() {
}); });
assertTrue(testWasRun); 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() { @Test public void runAClass() {
testWasRun= false; testWasRun= false;
JUnitCore.runClasses(Example.class); JUnitCore.runClasses(Example.class);
Expand Down

0 comments on commit d1f506e

Please sign in to comment.