I can find in the Maven docs where it shows how to run:
But how to run all the tests in a package? Is this possible?
I would prefer solutions that don't require modifying the pom.xml
or code.
We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.
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”.
mvn clean: Cleans the project and removes all files generated by the previous build. mvn compile: Compiles source code of the project. 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.
To run all the unit tests with Maven run command $ mvn test or $ mvn clean test . If you use clean , all the resources and compiled java code generated by maven in target directory will be cleaned and run tests freshly.
You could use a pattern as well, for example
mvn '-Dtest=de.mypackage.*Test' test
runs all tests in classes from package de.mypackage ending on *Test
.
[update 2017/12/18]:
Since this became the accepted answer, here's some further information:
The syntax used above (qualified package name) requires Surefire version 2.19.1
or higher! Earlier versions require the use of path expressions, for example
mvn -Dtest="de/mypackage/*Test" test
I'm using quotes (` or ") to prevent the shell from performing pathname expansion, Maven doesn't require any quotes.
A single test method can be exuted using the following syntax
mvn -Dtest=MyUnitTest#testMethod test
All tests from subpackages may be includes as well, in order to execute all tests in or beneath package de.mypackage.sub
execute:
mvn -Dtest="de/mypackage/sub/**" test
or with Surefire 2.19.1
or higher
mvn -Dtest="de.mypackage.sub.**" test
There are further possibilities like using regular expressions, see the official documentation of running a single test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With