How to run tests only for a single Maven module?
Asked Answered
M

2

10

Given a Maven project with modules dad and son, where son depends on dad, mvn -am -pl son test runs the tests for dad too. Is there a way to avoid this and only run tests for son module?

Note that there a couple of ways to achieve this, though each has its own caveats which I don't prefer:

  • Using -Dtest="**/son/*Test.java" -DfailIfNoTests=false overrides the maven-surefire-plugin config in the module.
  • Similar to -Dtest, JUnit 5 tags could also be filtered, but this approach suffers from the same shortcoming aforementioned.
  • One can first do an install -DskipTests=true and then mvn -pl son test, though this pollutes the local Maven repository with partial work.
Melidamelilot answered 25/11, 2020 at 12:25 Comment(13)
Initial mvn install for dad is the idiomatic way with Maven for this type of work. Though you also have an option to run them from IDE :)Net
So you want to compile "dad"'s classes but you don't want to run its tests, right?Mev
What about simply not using the -am cause it says: If project list is specified, also build projects required by the list...... Why do you do such things on command line and not within your ide? Apart from that what is so problematic to use the install .. and mvn -pl son test approach? So your local repository can be cleaned up easy? ...Running tests from a module or parts of it ... just using the IDE ... never doing that on command line ... the CI runs all tests on my branches...Officer
@StanislavBashkyrtsev Polluting ~/.m2 with partial work just to run the tests of a single module doesn't sound idiomatic to me.Cache
@MarkBramnik, right. To be honest, I don't even want to compile dad, though if you don't provide -am to compile the dependency modules, Maven complains that dad is not found.Cache
The $HOME/.m2/repository is a cache nothing else... polluting? There are then SNAPSHOT's etc.. So what?Officer
@VolkanYazıcı, it's local repo, it doesn't matter what you have here. It exists to be polluted since it may contain SNAPSHOTs.Net
Yes of course cause it's a dependency...than you have to go the way mvn install for the whole project. Than you can use mvn -pl son from the root which will resolve the dependency from your local cache...Officer
The cache is on your local machine which you can delete any time if it becomes to large ... then everything will be downloaded again... except we are talking about a setup for a CI solution like Jenkins ... I suppose we are talking about local development on dev machine...Officer
Regarding running from the IDE... IntelliJ IDEA 2019.3.5 cannot run tests for log4j-layout-template-json module due to its complex setup. Hence, it is not an option always. Thanks for your kind alternative work arounds, though in the question I explicitly stated that I want to avoid install and want to do this using mvn, not an IDE.Cache
@VolkanYazıcı I guess it is good to have alternative approaches because the thing you asked for is probably not really possible.Mats
If you can't run your tests from your IDE then the tests are too complex or wrongly done...and must be simplified/changed...Furthermore that is an indication to run such tests on a CI solution like Jenkins... Apart from that I strongly recommend to upgrade your IDE ... 2020.2 (2020.3 is around the corner already in beta)... Do not use old Software...The test sounds more like an integration tests ? Complex setup? What is the real problem here? Because you are insistence on not to use install but you do ???Officer
@JFabianMeier, sadly, I think so. Just wanted to double-check before creating a Maven ticket.Cache
T
6

On maven 3.3.9, I ran:

mvn test -pl :web-module

to ran the web module. In the POM, should look like this, <module>web-module</module>, inside the modules tag.

To test a specific class inside that module:

mvn test -pl :web-module -Dtest="ClassUnderTest"

Thermograph answered 21/2, 2023 at 10:51 Comment(1)
I get "Could not resolve dependencies for project my.group.id:my-artifact:jar:1.0-SNAPSHOT", where my-artifact is my dependency (dad in this post)Louth
A
2

I am stuck on this for a while, but eventually, I got the solution.

This is my condition:

  • I have many submodules: A, B, and C. some of them are independent, and some of them are dependent.
  • I want to only run the Module-C test, but C dependence on A and B.
  • mvn clean test -pl C does not work for me, because of 2.
  • mvn clean test -pl C -am work for me, but it runs all of the module tests.

This is my thought:

the reason why mvn clean test -pl C -am run all tests, is because C has a dependence on A and B, So module C must compile A and B first, and then compile C.

These is the Resolutions:

  1. make parent Pom skip all of the module tests.
  2. make a profile on Module C, and enable the test.
  3. just run mvn clean test -DmoduleCTest=true

The module C pom is like this, and another module is the same way but has diff profile id.

<profiles>
    <profile>
        <id>moduleCTest</id>
        <activation>
            <property>
                <name>moduleCTest</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skipTests>false</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Amidst answered 10/1, 2023 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.