문제

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, 이 모듈의 테스트를 실행하지 않습니다.

다른 팁

나는 이것이 더 쉽다고 생각하고 또한 비 surefire 테스트에서 일할 수있는 이점이 있습니다 (제 경우에는 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>

감사합니다 build-helper-maven-plugin 당신은 실제로 당신이 빌드 중에 특정 모듈에 있는지 여부를 동적으로 확인합니다. project.artifactId 속성 (각각을 가리키는 artifactId 빌드 중에 모듈), REGEX는 특정 값 (테스트를 건너 뛰려는 모듈 이름)에 대해 일치하는 것을 찾고 있습니다. maven.test.skip 그에 따라 속성 (설정 true).

이 경우 테스트가 건너 뜁니다 module1 그리고 module3 제대로 실행하는 동안 module2, 즉, Regex에 의해 표현 된 바와 같이.

이 접근법의 장점은 역동적이고 중앙 집중화되는 것입니다 (부모에게 pom.xml) 따라서 유지 보수에 더 나은 : 위의 간단한 정규를 변경하여 언제든지 모듈을 추가하거나 제거 할 수 있습니다.

분명히 이것이 빌드의 기본 동작이 아닌 경우 (권장 케이스), 항상 위의 스 니펫을 Maven 프로필.


더 나아가서 입력을 기반으로 동적 동작을 가질 수 있습니다.

<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>

여기서 우리는 실제로 REGEX 값을 속성으로 대체하고 있습니다. test.regex, 기본값은 none (또는 모듈 이름이나 기본 건너 뛰기 일치와 일치하지 않는 것은 무엇이든).

그런 다음 명령 줄에서 우리는 가질 수 있습니다

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)

즉, 런타임에 당신이 결정하고, 변경할 필요없이 pom.xml 프로필을 파일 또는 활성화합니다.

도움이 될 수있는이 질문과는 약간 다른 요구가있었습니다. 명령 줄에서 다른 패키지에서 몇 가지 다른 테스트를 제외하고 싶었으므로 단일 와일드 카드는 그렇게하지 않을 것입니다.

제외에 대한 Maven FailSafe 문서 규칙에서 쉼표로 구분 된 Regex 또는 Wildcard Exclusions를 지정할 수 있다는 것을 찾았습니다.https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html

그래서 내 pomfile은 다음과 같이 보였습니다.

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

그리고 내 명령 줄에는 다음이 포함되었습니다.

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

나에게 핵심 성분은 단일 제외 명세서에서 하나의 Maven 속성에 대한 많은 테스트를받는 것이 었습니다.

SureFire 플러그인 2.19 사용 정규 표현식을 사용하고 싶지 않은 테스트를 간단히 제외 할 수 있습니다.

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

위 명령은 포함 된 모든 테스트를 제외합니다. 제외.

NB1 Apostrophe ( ') 대신 이중 인용수 ( ")가 사용되는 경우 명령은 올바르게 해석되지 않으며 예상치 못한 결과를 생성합니다 (Bash 3.2.57을 사용하여 테스트).

NB2 여러 버전의 SureFire 플러그인이 사용되는 프로젝트에 특히주의를 기울여야합니다. 2.19 이상의 Surefire 버전은 정규식을 지원하지 않기 때문에 테스트를 실행하지 않습니다.

버전 관리 (부모 POM 파일에 이것을 추가하는 것이 좋습니다) : :

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

테스트를 건너 뛰는 빌드 명령의 예 : https://artbcode.wordpress.com/2016/11/28/how-to-skip-a-subset-of-the-unit-tests/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top