107

Is there any way to provide some command-line argument in order to skip all tests but one on some module? So I will not need to change pom.xml every time I will need to run another test?

For example, I want to create build configuration on TeamCity, and provide command-line arguments to run only single test in some module. Next time I will need to change it and run another test, and so on.

Perhaps it is not how CI is intended to be used, but still.

jdevelop
  • 12,176
  • 10
  • 56
  • 112
  • 1
    Did you see this topic? http://stackoverflow.com/questions/1873995/run-a-single-test-method-with-maven and here some more: http://maven.apache.org/plugins/maven-surefire-plugin/examples/single-test.html other than these I could not find. – Mauno Vähä Aug 08 '12 at 17:20
  • 11
    @MaunoV.Actually if there are several modules, the test will fail. I found the solution: -Dtest=TestName -DfailIfNoTests=false – jdevelop Aug 08 '12 at 18:30

4 Answers4

137

I assume you've read the docs about running a single test under surefire? What they don't tell you is how to do that in a sub-module:

mvn test -Dtest=testname -pl subproject

Where subproject is the project containing that test. From the mvn man page:

-pl,--projects arg Comma-delimited list of specified reactor projects to build instead of all projects. A project can be specified by [groupId]:artifactId or by its relative path.

Nick Gerner
  • 1,849
  • 1
  • 15
  • 9
  • I wish there was a way of doing it without specifying the subproject so I don't have to think about it (I could do a bash for but it's ugly) – Ciro Santilli OurBigBook.com Mar 19 '15 at 15:42
  • 46
    This solution doesn't work if the module to be tested depends on other subprojects. – Vadzim Nov 16 '15 at 08:06
  • 13
    @Vadzim, for me it helped to use `-DfailIfNoTests=false`: `mvn test -Dtest=testname -DfailIfNoTests=false` – Abdull May 23 '16 at 15:37
  • If the module depends on other submodules, run `mvn install` on the parent project. The dependencies will be installed to your system and maven will find them when you run `mvn test` on the submodule. – Matthew Sep 12 '17 at 19:17
  • 17
    you can also add -am (short for --also-make) to also build dependencies, if the sub-project you want to test depends on other sub-projects – Jack Davidson Oct 08 '17 at 04:26
  • 1
    @CiroSantilli刘晓波死六四事件法轮功 There is. Please see [this post](https://stackoverflow.com/questions/11869762/maven-run-only-single-test-in-multi-module-project/47493510#47493510). – Debosmit Ray Nov 26 '17 at 06:04
  • 1
    @Vadzim @Abdull , `-DfailIfNoTests=false` will not work if the project is using the Surefire plugin to run tests. You should use `-Dsurefire.failIfNoSpecifiedTests=false` instead – Ahmadreza Dec 08 '22 at 16:13
53

Other answers I see are not fully complete, for projects that depend on other sub-modules to be built. One option is to run mvn install to have the required jars to be installed into ~/.m2/..., but that option is not very "clean".

Following command will build the sub-modules, and run only the test class that is specified. This is to be run at parent module level. Also, no need to specify sub-module name.

mvn test -DfailIfNoTests=false -Dtest={test_class_name} -am

As an aside, this can also be mvn clean test -Dfa...... I have a habit of always running clean when running tests.

References..
-am will make all the other sub-modules.
-DfailIfNoTests=false does not fail the entire process since we are not intending to run tests in other modules.
-pl option is not needed since -am is already building everything

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
  • 2
    I find it runs slightly faster if I include the "-pl projectname". – Curtis Yallop Mar 18 '19 at 20:47
  • Is there a way to run all the tests in a submodule and also build all the modules it depends on? Something like mvn test -DfailIfNoTests=false -Dtest={moduleName} -am – Anon Nov 08 '19 at 09:15
  • @Anon wouldn't the comment from `@CurtisYallop` achieve that? – Debosmit Ray Nov 08 '19 at 16:16
  • @DebosmitRay No. If I want to run tests from module1-test and module1-test depends on module2-test, then mvn test -pl module1-test -am will run tests from module2-test as well. – Anon Nov 14 '19 at 14:06
  • 2
    Thanks for the answer. saved me a headache. – Debadatta Aug 19 '20 at 15:00
  • 1
    This is definitely the right answer. The one currently marked as the correct one only works if you install artefacts to your local `.m2/repository` folder, which is something you do not want to do, especially if you are working on a multi module project. In fact, not having to `mvn install` is one of the main reasons to use multi modules. On top of that, this answer conveniently allows one not to have to specify the project, which in 99.99% of cases is redundant. – Akira Feb 04 '21 at 18:33
15

In case the module to be tested depends on other projects, solution works by changing commands as:

mvn test -DfailIfNoTests=false -Dtest=testname -pl subproject
user229044
  • 232,980
  • 40
  • 330
  • 338
Lalit Narnaulia
  • 159
  • 1
  • 4
1

FWIW, if you have a multi-module project, you can run all tests with this command at parent directory.

mvn test -pl subproject

And the subproject's name can be found by running the following command, usually in the form of group-id:artifact-id.

mvn help:active-profiles
MichaelZ
  • 1,890
  • 2
  • 13
  • 8