Fork me on GitHub

Running a Single Test

During development, you may run a single test class repeatedly. To run this through Maven, set the test property to a specific test case.

mvn -Dtest=TestCircle test

If you have multiple executions configured in surefire plugin within your POM, you may want to execute the only default test phase:

mvn surefire:test -Dtest=TestCircle

The value for the test parameter is the name of the test class (without the extension; we'll strip off the extension if you accidentally provide one).

You may also use patterns to run a number of tests:

mvn -Dtest=TestCi*le test

And you may use multiple names/patterns, separated by commas:

mvn -Dtest=TestSquare,TestCi*le test

NOTE: Use syntax e.g. "foo/MyTest.java", "**/MyTest.java", "MyTest" for "test" parameter (see includes/excludes).

Running a Set of Methods in a Single Test Class

As of Surefire 2.7.3, you can also run only a subset of the tests in a test class.

NOTE : This feature is supported only for Junit 4.x and TestNG. Use syntax e.g. "foo/MyTest.java", "**/MyTest.java", "MyTest" for "test" parameter (see includes/excludes).

You should use the following syntax:

mvn -Dtest=TestCircle#mytest test

You can use patterns too

mvn -Dtest=TestCircle#test* test

Since of Surefire Plugin 2.19 you can select multiple methods (JUnit 4, JUnit 4.7+ and TestNG):

mvn -Dtest=TestCircle#testOne+testTwo test

Note this feature was available in JUnit 4 provider only since of Surefire Plugin 2.12.1.

Multiple Formats in One

As of Surefire Plugin 2.19 multiple formats are supported in one pattern (JUnit 4, JUnit 4.7+, TestNG):

mvn "-Dtest=???Test, !Unstable*, pkg/**/Ci*leTest.java, *Test#test*One+testTwo?????, #fast*+slowTest" test
mvn "-Dtest=Basic*, !%regex[.*.Unstable.*], !%regex[.*.MyTest.class#one.*|two.*], %regex[#fast.*|slow.*]" test

The exclamation mark (!) excludes tests. The character (?) within non-regex pattern replaces one character in file name or path. The file extensions are not mandatory in non-regex patterns, and packages with slash can be used. The regex validates fully qualified class file, and validates test methods separately after (#) however class is optional. The regex supports '.class' file extension only. Note the regex comments, marked by (#) character, are unsupported. The Parameterized JUnit runner describes test methods using an index in brackets, so the non-regex method pattern would become #testMethod[*]. If using the JUnit annotation @Parameters(name="{index}: fib({0})={1}") and selecting the index e.g. 5 in pattern, the non-regex method pattern would become #testMethod[5:*].

Fully qualified class name

As of Surefire Plugin 2.19.1, the syntax with fully qualified class names or packages can be used, e.g.:

<test>my.package.???Test#testMethod, another.package.*</test>

The character (?) replaces single character and (*) represents zero or more characters. Multiple formats can be additionally combined.