Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple test classes or test methods using Maven?

Tags:

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?

like image 313
Alpha Avatar asked Dec 26 '14 06:12

Alpha


People also ask

How can you run test classes in Maven?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.

Which of the following Maven command is used to run the test cases?

1) Run all testcases with command “mvn test” : This command run all testcases present inside test folder irrespective of any other criteria.

What does mvn test command do?

mvn test-compile: Compiles the test source code. mvn test: Runs tests for the project. mvn package: Creates JAR or WAR file for the project to convert it into a distributable format. mvn install: Deploys the packaged JAR/ WAR file to the local repository.


2 Answers

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

like image 83
Subir Kumar Sao Avatar answered Sep 28 '22 04:09

Subir Kumar Sao


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)

like image 30
jorrocks Avatar answered Sep 28 '22 05:09

jorrocks