From d1f506e0915874a0388af3f87c791a95834f8d1b Mon Sep 17 00:00:00 2001 From: schaarsc Date: Tue, 31 Jul 2012 21:04:40 +0200 Subject: [PATCH] add command line parameter to run single method. --- src/main/java/org/junit/runner/JUnitCore.java | 61 ++++++++++++++++++- .../tests/running/core/CommandLineTest.java | 41 ++++++++++++- 2 files changed, 99 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/junit/runner/JUnitCore.java b/src/main/java/org/junit/runner/JUnitCore.java index d70f03606f13..7acbce2ac32e 100644 --- a/src/main/java/org/junit/runner/JUnitCore.java +++ b/src/main/java/org/junit/runner/JUnitCore.java @@ -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 methods = new ArrayList(); List> classes= new ArrayList>(); List missingClasses= new ArrayList(); 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; @@ -103,6 +122,44 @@ public String getVersion() { return Version.id(); } + /** + * Construct new {@link Filter} based on method list. + * @param methods + * @return filter to run only methods listed in methods + */ + private Filter createMethodFilter(final List 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 classes 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 classes. * @param classes the classes containing tests diff --git a/src/test/java/org/junit/tests/running/core/CommandLineTest.java b/src/test/java/org/junit/tests/running/core/CommandLineTest.java index 971ce1cd05b7..47c0ab1dea1d 100644 --- a/src/test/java/org/junit/tests/running/core/CommandLineTest.java +++ b/src/test/java/org/junit/tests/running/core/CommandLineTest.java @@ -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 @@ public class CommandLineTest { 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 @@ static public class Example { @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);