Currently, when a JUnit test is executed via the Ant JUnit task ( http://ant.apache.org/manual/OptionalTasks/junit.html ), a number of test methods in the test class are executed and there is no support in the JUnit task for instructing JUnit to execute a single test method (like the way JUnit provides this functionality via the -m parameter in the junit.textui.TestRunner class). This patch (generated against the ANT_16_BRANCH in CVS) provides the ability to specify a test method name to be executed in the new "method" attribute of the nested "test" element in the <junit> ant task. Reasons why enhancing the Ant JUnit task to allow the execution a single test method is desirable: * When a test method fails, you can run the failing test method under the Java debugger without having to wait for the other test methods to complete (especially where some of the other tests are long running). * Ability to quickly test code changes that fix a failing test * Ability to run under a Java profiler, a single test method that appears to be running slower than expected (without the overhead of running the profiler whilst other test methods are being executed). * Speeds up the test/debug cycle when you are running on a busy system or slow hardware * No need to make code changes to force one test to be run when you want to debug a single test. The patch also adds 2 tests for the new functionality, which have been executed along with the existing Ant JUnit tests in the ANT_16_BRANCH.
Created attachment 14935 [details] [PATCH] JUnit - add ability to execute a single test method
FYI.. If this patch is accepted, I plan to submit a related patch to the Apache Maven project's Test plug-in that allows this new functionality to be used in a command like the following: maven -o -Dtestcasecom.mycompany.MyTest -Dmethod=testSomething test:single or under a debugger.. maven -o -Dmaven.junit.jvmargs="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000" -Dtestcasecom.mycompany.MyTest -Dmethod=testSomething test:single
* please use ant head and not ant_16_branch * the patch changes a public constuctor - JUnitTest
Created attachment 14963 [details] [PATCH] JUnit - add ability to execute a single test method Attached patch (generated against cvshead) for review that obsoletes the existing patch that I submitted (that was generated against the ant_16_branch). The 'since' information in the doc & javadoc changes assume that it will be included in Ant 1.7. I have checked that no public method signatures have been changed. Also see the two // TODO comments - delete them if you don't think a different message should be logged (I wasn't sure whether changing the messages could impact some program that relies upon the format of the log messages).
Any chance someone could review this patch. Thanks.
The patch looks good at the first sight (but no thorough check yet). I suggest that it should be possible to specify multiple method names.
Would also want to ensure that this works sanely with JUnit 4 tests using annotations. I presume that if the test class has a public static Test suite() {...} method, the parameter would be ignored?
Why do you think it should be ignored? I think it should not be ignored.
If you have a static suite method, then that is going to specify a list of tests to run (which may in fact live in other classes, etc.), rather than the default suite which contains all test* methods from the current class. Using suite() would directly contradict the intent to run a single test case from this class, as I understand it.
I understand what the suite() method is for but I still I do not understand what would be the reason for ignoring the method name if the suite() method is present. What would be the benefit?
If you have public class MyTest extends TestCase { public void test1() {...} public void test2() {...} } then it makes sense to ask to run test1 by itself, or test2 by itself; the implicit default suite being run is test1 followed by test2, which the environment is just overriding. But if you have public class MyTest extends TestCase { public MyTest(String n) { super(n); } public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new MyTest("one")); suite.addTest(new MyTest("two")); // stuff in other classes too, maybe parametrized: suite.addTest(new MyOtherTestSuite()); return suite; } public void one() {...} /* expected to be run immediately after one() */ public void two() {...} } then the suite() method gives the definitive answer to what tests should be run and in which order. In this case it is senseless to try to run a single test method from the environment.
I understand. But I still consider it a needless restriction - something like "I assume you will not need to do this so I will not allow you to do it." In other words: Why it is better to have this restriction in place than not to have it? What would be the negative implications if the restriction is not applied? Another reasoning: If a test class does not contain any suite() method, then the set of test methods is defined. By specifying a particular method name (or names), I override this selection. Why it should be any different if the suite() method is present?
Well, you can try to select some "public void test*()" methods to run even if there is a suite() method, but there might not be any such methods (they might be implemented in another class, or named differently), and they might not run correctly. I guess it is harmless to try.
What's the current thinking on this patch? Thanks, David Saff
FYI: a variant of this patch (against 1.7.1) is shipped with NetBeans 6.8 to permit single test method execution from inside the IDE. (Versioned project build scripts do not use this feature - only available for "quick run" mode with a special transient script.) https://svn.apache.org/repos/asf/ant/core/branches/run-single-test-method/ holds an update of the patch against 1.8.0.
Branch updated to 1.8.1.
I would like to merge the run-single-test-method branch for 1.8.2. It has been in use for quite some time as a binary patch in NetBeans, for rerunning failed tests. Any objections to merging it?
(In reply to comment #17) > I would like to merge the run-single-test-method branch for 1.8.2. It has been > in use for quite some time as a binary patch in NetBeans, for rerunning failed > tests. Any objections to merging it? Jesse, please do merge
merged: rev 34748