10

Can you run a test for a single method only, not a whole class? Ideally from the command line.

e.g. for a class:

public class TestClass extends Unittest {
  @Test
  public void test1_shouldbeRun() {

  }

  @Test
  public void test2_shouldNotBeRun() {

  } 
}

I only want to run the method "test1_shouldBeRun".

risingTide
  • 1,754
  • 7
  • 31
  • 60
forste
  • 1,103
  • 3
  • 14
  • 33

5 Answers5

7

It is possible when you use the surefire plugin [1]:

mvn -Dtest=TestClass#test1_shouldbeRun test

[1] http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

fabb
  • 11,660
  • 13
  • 67
  • 111
5

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."
rlegendi
  • 10,466
  • 3
  • 38
  • 50
  • thanks, even though the eclipse option seems runs all tests of the class it still helps me. – forste Mar 11 '12 at 13:22
  • however, the application log does not get logged to the eclipse console, do you know how to direct it there? – forste Mar 11 '12 at 13:23
  • apparently the log level is somehow set to INFO, thus I am experiencing a problem similar to http://stackoverflow.com/questions/3501355/log4j-output-not-displayed-in-eclipse-console – forste Mar 11 '12 at 13:34
  • Eclipse (3.7.2) can run Unit tests separately for sure (even previous versions, but 3.4 had some issues regarding to this feature). – rlegendi Mar 11 '12 at 18:15
  • it can run tests separately generally or for the play framework? – forste Mar 12 '12 at 10:29
  • It's a built-in Eclipse feature. See here http://stackoverflow.com/questions/646307/running-a-single-junit-test-in-eclipse or here http://srivaths.blogspot.com/2009/04/run-single-junit-test-method-in-eclipse.html – rlegendi Mar 13 '12 at 08:29
  • thanks. so I guess you are NOT using it in combination with the play framework since I all other ressources I have found suggest that you can not run single test methods (using play) even inside eclipse with the junit runner – forste Mar 13 '12 at 15:24
  • No, I'm not using the Play framework (it was only mentioned by a comment of yours yesterday). – rlegendi Mar 13 '12 at 16:10
1

I made a post about this a little while back.

This is possible using JUnit 4 and Ant. The trick is pass the method name[s] as a property on the command line and use a different task if that property was passed.

Given this test class:

import org.junit.Test;

public class FooTests {

  @Test
  public void firstTest() {
    System.out.println("test 1");
  }

  @Test
  public void secondTest() {
    System.out.println("test 2");
  }

  @Test
  public void thirdTest() {
    System.out.println("test 3");
  }

}

Create this ant file:

<project default="test">

    <!-- Tell Ant where to find JUnit -->
    <path id="classpath.test">
        <pathelement location="junit-4.11.jar" />
        <pathelement location="hamcrest-core-1.3.jar" />
        <pathelement location="." /> <!-- or where ever your test class file is -->
    </path>
    <target name="test" description="Runs the tests">
        <!-- Set a new Ant property "use-methods" to true if the "test-methods" Ant property - which we'll pass in from the command line - exists and is not blank.-->
        <condition property="use-methods" else="false">
            <and>
                <isset property="test-methods"/>
                <not>
                    <equals arg1="${test-methods}" arg2=""/>
                </not>
            </and>
        </condition>

        <!-- Sanity check -->
        <echo message="use-methods = ${use-methods}"/>
        <echo message="test-methods = ${test-methods}"/>

        <!-- Run the tests -->
        <junit>
            <classpath refid="classpath.test" />
            <formatter type="brief" usefile="false" />
            <!-- The "if" tells JUnit to only run the test task if the use-methods property is true.-->
            <test if="${use-methods}" name="FooTests" methods="${test-methods}"/>
            <!-- The "unless" tells JUnit to not run the test task if the use-methods property is true.-->
            <test unless="${use-methods}" name="FooTests"/>
        </junit>
    </target>
</project>

Examples

Run all tests.

$ ant
Buildfile: build.xml

test:
     [echo] use-methods = false
     [echo] test-methods = ${test-methods}
    [junit] Testsuite: FooTests
    [junit] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
    [junit]
    [junit] ------------- Standard Output ---------------
    [junit] test 3
    [junit] test 1
    [junit] test 2
    [junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL
Total time: 0 seconds

Run only the second test by specifying the “test-methods” Ant property on the command line.

$ ant -Dtest-methods=secondTest
Buildfile: build.xml

test:
     [echo] use-methods = true
     [echo] test-methods = secondTest
    [junit] Testsuite: FooTests
    [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 sec
    [junit]
    [junit] ------------- Standard Output ---------------
    [junit] test 2
    [junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL
Total time: 0 seconds

Run only the second and third tests.

$ ant -Dtest-methods=secondTest,thirdTest
Buildfile: build.xml

test:
     [echo] use-methods = true
     [echo] test-methods = secondTest,thirdTest
    [junit] Testsuite: FooTests
    [junit] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec
    [junit]
    [junit] ------------- Standard Output ---------------
    [junit] test 3
    [junit] test 2
    [junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL
Total time: 0 seconds
Matt Miller
  • 162
  • 1
  • 11
0

there is Jute plugin, published in maven central, the plugin allows execute JUnit test methods as external separated Java processes and you can define methods which will be included and excluded from test process and if to define only one test method as included then only the test method will be executed

Igor Maznitsa
  • 833
  • 7
  • 12
0

Don't know if this works on your machine/library version, but using the private modifier on methods that I don't want to test worked out for me

Ngọc Hy
  • 298
  • 1
  • 6
  • 15