UFO ET IT

Maven의 일부 모듈에서 테스트 건너 뛰기

ufoet 2020. 11. 17. 21:28
반응형

Maven의 일부 모듈에서 테스트 건너 뛰기


Maven 빌드에서 대부분의 단위 테스트를 실행하고 싶습니다. 그러나 한 프로젝트에는 더 느린 단위 테스트가 있으며 일반적으로 제외하고 싶습니다. 때때로 켜십시오.

질문 : 어떻게해야합니까?

에 대해 알고 -Dmaven.test.skip=true있지만 모든 단위 테스트를 해제합니다.

여기 에 설명 된 통합 테스트 건너 뛰기에 대해서도 알고 있습니다 . 하지만 통합 테스트가없고 단위 테스트 만 있고 maven-surefire-plugin에 대한 명시적인 호출이 없습니다. (Eclipse-Maven 플러그인과 함께 Maven 2를 사용하고 있습니다).


이 모듈에서만 테스트를 건너 뛰는 것은 어떻습니까?

이 모듈의 pom.xml에서 :

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.4.2</version>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

결국 테스트를 비활성화하는 프로필을 만들 수 있습니다 (여전히 모듈의 pom.xml).

<project>
  [...]
  <profiles>
    <profile>
      <id>noTest</id>
      <activation>
        <property>
          <name>noTest</name>
          <value>true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.2</version>
            <configuration>
              <skipTests>true</skipTests>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  [...]
</project>

후자의 솔루션을 사용하면을 실행하면 mvn clean package모든 테스트가 실행됩니다. 를 실행하면 mvn clean package -DnoTest=true이 모듈에 대한 테스트가 실행되지 않습니다.


나는 이것이 더 쉽다고 생각하며 확실하지 않은 테스트 (제 경우에는 FlexUnitTests) 작업의 이점도 있습니다.

<profile>
   <id>noTest</id>
    <properties>
       <maven.test.skip>true</maven.test.skip>
    </properties>
 </profile>

대규모 다중 모듈 프로젝트가 있고 pom.xml사용자 지정 구성 및 프로파일 링 을 사용하여 각 모듈 파일 을 변경할 필요없이 특정 모듈에서만 테스트를 건너 뛰 려면 다음을 상위 pom.xml파일에 추가 할 수 있습니다 .

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.12</version>
            <executions>
                <execution>
                    <id>regex-property</id>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>maven.test.skip</name>
                        <value>${project.artifactId}</value>
                        <regex>(module1)|(module3)</regex>
                        <replacement>true</replacement>
                        <failIfNoMatch>false</failIfNoMatch>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<modules>
    <module>module1</module>
    <module>module2</module>
    <module>module3</module>
</modules>

Thanks to the build-helper-maven-plugin you would actually dynamically check whether you are in a certain module or not during the build, via the project.artifactId property (pointing at each artifactId module during the build), the regex would then seek matching for certain values (the module names for which you want to skip tests) and populated the maven.test.skip property accordingly (setting it to true).

In this case, tests will be skipped for module1 and module3 while running properly for module2, that is, as expressed by the regex.

The advantage of this approach is to have it dynamic and centralized (in the parent pom.xml) hence better for maintenance: you could add or remove modules at any time simply by changing the simple regex above.

Obviously, if this is not the default behavior of the build (recommended case), you could always wrap the snippet above in a maven profile.


You could also go further and have dynamic behavior based on your input:

<properties>
    <test.regex>none</test.regex>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.12</version>
            <executions>
                <execution>
                    <id>regex-property</id>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>maven.test.skip</name>
                        <value>${project.artifactId}</value>
                        <regex>${test.regex}</regex>
                        <replacement>true</replacement>
                        <failIfNoMatch>false</failIfNoMatch>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Here we are actually replacing the regex value with a property, test.regex, with default value to none (or whatever would not match any module name or, also, the default skipping matchings required).

Then from command line we could have

mvn clean test -Dtest.regex="(module1)" > will skip tests only for module1
mvn clean test -Dtest.regex="(module1)|(module2)" > will skip tests on module1 and module2
mvn clean test -Dtest.regex="(module1)|(module2)|(module3)" > will skip the three module tests
mvn clean test -Dtest.regex=".+" > will skip all module tests
mvn clean test > would not skip anything (or fall back on default behavior)

That is, then at runtime you decide, without any need to change the pom.xml file or activating any profile.


I had a slightly different need from this question that may prove helpful. I wanted to exclude from the command line a few different tests from different packages, so a single wildcard would not do it.

I found in the Maven Failsafe documentation rules for exclusions that you can specify a comma-separated list of either regex or wildcard exclusions: https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html

So my pomfile looked like this:

<excludes>
    <exclude>${exclude.slow.tests}</exclude>
</excludes>

and my command line included this:

mvn install "-Dexclude.slow.tests=**/SlowTest1.java, **/package/ofslowtests/*.java, **/OtherSlowTest.java"

For me the key ingredient was getting a bunch of tests into one maven property in a single exclude statement.


Using Surefire Plugin 2.19 you can simply exclude the tests you don't want using regular expressions:

mvn '-Dtest=!%regex[.*excludedString.*]' test

The above command will exclude all the tests that contain excludedString.

NB1 If double quotation mark(") is used instead of apostrophe(') the command will not be interpreted properly and will produce unexpected results. (Tested using bash 3.2.57)

NB2 Particular attention should be paid to projects in which multiple version of the surefire plugin is used. Versions of surefire older than 2.19 will not execute any tests because they do not support regular expressions.

Version management(it might be a good idea to add this in the parent pom file):

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

Examples of build commands that skip tests: https://artbcode.wordpress.com/2016/11/28/how-to-skip-a-subset-of-the-unit-tests/

참고URL : https://stackoverflow.com/questions/772735/skipping-tests-in-some-modules-in-maven

반응형