24

In order to run all Maven tests, we can use:

mvn clean test

If we want to run specific test class, we can use:

mvn clean test -Dtest=className

If we want to run specific method from specific test class, we can use:

mvn clean test -Dtest=className#methodName

But I want to run:

  1. multiple test classes(not all that belong to src\test\java)
  2. multiple test methods from specific test class(not all test methods of specific test class that belong to src\test\java)

Are there Maven commands using which I can achieve above two?

Alpha
  • 13,320
  • 27
  • 96
  • 163
  • If you are using Surefire plugin you can see the documentation. http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html – Subir Kumar Sao Dec 26 '14 at 06:22
  • an include/exclude (if using surefire) with a pattern is a possibility (and some profiles) –  Dec 26 '14 at 06:23

4 Answers4

37

If using surefire plugin then you may use the below options.

For multiple classes you can use,

mvn -Dtest=TestSquare,TestCi*le test

For multiple methods in same class you can use,

mvn -Dtest=TestCircle#testOne+testTwo test

Refer docs

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
  • Thanks Subir for the answer! I can run multiple test classes as you suggested, but I can't run multiple methods in same class as per your suggestion. I've `surefire-plugin 2.18` and `testng 6.8.13`. Both are with latest version. – Alpha Dec 26 '14 at 14:51
  • Same for me, I get this exception: JUnit4Provider.invoke:128->executeTestSet:147->getMethod:307 » StringIndexOutOfBounds – Alessandro Polverini Jun 10 '15 at 08:36
  • When I try to run the multiple classes command (and only with that) I get the following error `[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project: No tests were executed!` – Marco Lackovic Oct 23 '20 at 14:43
13

You can use wildcards - note that you have to quote the test argument so the shell does not expand the wildcard.

mvn -Dtest="TestSquare,TestCi*le" test

(using maven-surefire-plugin:2.17)

metadaddy
  • 4,234
  • 1
  • 22
  • 46
jorrocks
  • 161
  • 1
  • 4
2

If You want to launch all test Clases from subdirectory, eg: /doc/ You can use command:

mvn -Dtest=*/doc/* test
0

You can add multiple classes in TestNG with their groups, like

<groups>
  <run>
    <include name = "checkintest" />
    <include name = "videoSpider" />
    <include name = "xmlTCUploader" />
    <include name = "PALLogin" />
  </run>
</groups>
<classes>
  <class name="SeleniumUC"/>
  <class name="PALTestCasesSuite"/>
</classes>

After this, you can use these group with Maven like -

mvn -Dgroups=PALLogin test
Sidharth Taneja
  • 548
  • 6
  • 7