Home » Java » Run a single test method with maven

Run a single test method with maven

Posted by: admin November 2, 2017 Leave a comment

Questions:

I know you can run all the tests in a certain class using:

mvn test -Dtest=classname

But I want to run an individual method and -Dtest=classname.methodname doesn’t seem to work.

Answers:

To run a single test method in Maven, you need to provide the command as:

mvn -Dtest=TestCircle#xyz test

where TestCircle is the test class name and xyz is the test method.

Wild card characters also work; both in the method name and class name.

If you’re testing in a multi-module project, specify the module that the test is in with -pl <module-name>.

For integration tests use it.test=... option instead of test=...:

mvn -pl <module-name> -Dit.test=TestCircle#xyz integration-test

Questions:
Answers:

There is an issue with surefire 2.12.
This is what happen to me changing maven-surefire-plugin from 2.12 to 2.11:

  1. mvn test -Dtest=DesignRulesTest

    Result:
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project pmd: No tests were executed!

  2. mvn test -Dtest=DesignRulesTest

    Result:
    [INFO] — maven-surefire-plugin:2.11:test (default-test) @ pmd —

    Running net.sourceforge.pmd.lang.java.rule.design.DesignRulesTest
    Tests run: 5, Failures: 0, Errors: 0, Skipped: 4, Time elapsed: 4.009 sec

Questions:
Answers:

What I do with my TestNG, (sorry, JUnit doesn’t support this) test cases is I can assign a group to the test I want to run

@Test(groups="broken")

And then simply run ‘mvn -Dgroups=broken’.

Questions:
Answers:

Running a set of methods in a Single Test Class
With version 2.7.3, you can run only n tests in a single Test Class.

NOTE : it’s supported for junit 4.x and TestNG.

You must use the following syntax

mvn -Dtest=TestCircle#mytest test

You can use patterns too

mvn -Dtest=TestCircle#test* test

As of surefire 2.12.1, you can select multiple methods (JUnit4X only at this time, patches welcome)

mvn -Dtest=TestCircle#testOne+testTwo test

Check this link about single tests

Questions:
Answers:

New versions of JUnit contains the Categories runner:
http://kentbeck.github.com/junit/doc/ReleaseNotes4.8.html

But releasing procedure of JUnit is not maven based, so maven users have to put it manually to their repositories.

Questions:
Answers:

The test parameter mentioned by tobrien allows you to specify a method using a # before the method name. This should work for JUnit and TestNG. I’ve never tried it, just read it on the Surefire Plugin page:

Specify this parameter to run individual tests by file name, overriding the includes/excludes parameters. Each pattern you specify here will be used to create an include pattern formatted like **/${test}.java, so you can just type “-Dtest=MyTest” to run a single test called “foo/MyTest.java”.
This parameter overrides the includes/excludes parameters, and the TestNG suiteXmlFiles parameter. since 2.7.3 You can execute a limited number of method in the test with adding #myMethod or #my*ethod. Si type “-Dtest=MyTest#myMethod” supported for junit 4.x and testNg

Questions:
Answers:

This command works !!
mvn "-DTest=JoinTeamTestCases#validateJoinTeam" test Note that “-DTest” starts with UPPER CASE ‘T’.

Questions:
Answers:

You can run a single test class, but not a single method within a test class. You use the simple name of the class not the fully-qualified name of the class. So, if you have a test in “org.sonatype.test.MyTest” and that is the only test you want to run, your command line would look like this:

mvn test -Dtest=MyTest

Questions:
Answers:

To my knowledge, the surefire plugin doesn’t provide any way to do this. But feel free to open an issue 🙂