[java] Maven Integration 테스트를 실행하려면 어떻게합니까

나는 maven2 멀티 모듈 프로젝트를 가지고 내 아이 모듈의 각 난 명명 된 JUnit 테스트가 Test.javaIntegration.java각각 단위 테스트 및 통합 테스트에 대한합니다. 내가 실행할 때 :

mvn test

*Test.java하위 모듈 내의 모든 JUnit 테스트 가 실행됩니다. 내가 실행할 때

mvn test -Dtest=**/*Integration

Integration.java하위 모듈 내 에서 테스트가 실행 되지 않습니다 .

이것들은 나에게 똑같은 명령처럼 보이지만 -Dtest = / * Integration ** 인 명령은 작동하지 않습니다. 부모 수준에서 0 개의 테스트가 실행되고 테스트는 없습니다.



답변

단위 테스트와 통합 테스트를 개별적으로 실행하도록 Maven의 Surefire를 설정할 수 있습니다. 표준 단위 테스트 단계에서는 통합 테스트와 패턴이 일치하지 않는 모든 것을 실행합니다. 그런 다음 통합 테스트 만 실행 하는 두 번째 테스트 단계작성하십시오 .

예를 들면 다음과 같습니다.

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>**/*IntegrationTest.java</exclude>
        </excludes>
      </configuration>
      <executions>
        <execution>
          <id>integration-test</id>
          <goals>
            <goal>test</goal>
          </goals>
          <phase>integration-test</phase>
          <configuration>
            <excludes>
              <exclude>none</exclude>
            </excludes>
            <includes>
              <include>**/*IntegrationTest.java</include>
            </includes>
          </configuration>
        </execution>
      </executions>
    </plugin>


답변

메이븐 빌드 라이프 사이클은 이제 “테스트”단계에서 실행하는 단위 테스트와는 별도로 실행되는 통합 테스트를 실행하기위한 “통합 테스트”단계가 포함되어 있습니다. “package”후에 실행되므로 “mvn verify”, “mvn install”또는 “mvn deploy”를 실행하면 통합 테스트가 진행됩니다.

기본적으로, 통합 테스트라는 테스트 클래스를 실행 **/IT*.java, **/*IT.java그리고 **/*ITCase.java, 그러나 이것은 구성 할 수 있습니다.

이 모든 것을 연결하는 방법에 대한 자세한 내용은 Failsafe 플러그인 , Failsafe 사용 페이지 (이전 페이지에서 올바르게 링크되지 않음) 및 Sonatype 블로그 게시물을 참조하십시오 .


답변

나는 당신이하고 싶은 일을 정확하게 수행했으며 훌륭하게 작동합니다. 단위 테스트 “* Tests”는 항상 실행되며 “* IntegrationTests”는 mvn verify 또는 mvn install을 수행 할 때만 실행됩니다. 여기 내 POM의 스 니펫입니다. serg10은 거의 옳았습니다.

  <plugin>
    <!-- Separates the unit tests from the integration tests. -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
       <!-- Skip the default running of this plug-in (or everything is run twice...see below) -->
       <skip>true</skip>
       <!-- Show 100% of the lines from the stack trace (doesn't work) -->
       <trimStackTrace>false</trimStackTrace>
    </configuration>
    <executions>
       <execution>
          <id>unit-tests</id>
          <phase>test</phase>
          <goals>
             <goal>test</goal>
          </goals>
          <configuration>
                <!-- Never skip running the tests when the test phase is invoked -->
                <skip>false</skip>
             <includes>
                   <!-- Include unit tests within integration-test phase. -->
                <include>**/*Tests.java</include>
             </includes>
             <excludes>
               <!-- Exclude integration tests within (unit) test phase. -->
                <exclude>**/*IntegrationTests.java</exclude>
            </excludes>
          </configuration>
       </execution>
       <execution>
          <id>integration-tests</id>
          <phase>integration-test</phase>
          <goals>
             <goal>test</goal>
          </goals>
          <configuration>
            <!-- Never skip running the tests when the integration-test phase is invoked -->
             <skip>false</skip>
             <includes>
               <!-- Include integration tests within integration-test phase. -->
               <include>**/*IntegrationTests.java</include>
             </includes>
          </configuration>
       </execution>
    </executions>
  </plugin>

행운을 빕니다!


답변

JUnit 카테고리와 Maven을 사용하여 매우 쉽게 분할 할 수 있습니다.
이것은 단위 및 통합 테스트를 분할하여 아래에 매우 간략하게 표시됩니다.

마커 인터페이스 정의

카테고리를 사용하여 테스트를 그룹화하는 첫 번째 단계는 마커 인터페이스를 만드는 것입니다.
이 인터페이스는 통합 테스트로 실행하려는 모든 테스트를 표시하는 데 사용됩니다.

public interface IntegrationTest {}

테스트 클래스를 표시하십시오

테스트 클래스 상단에 카테고리 주석을 추가하십시오. 새 인터페이스의 이름을 사용합니다.

import org.junit.experimental.categories.Category;

@Category(IntegrationTest.class)
public class ExampleIntegrationTest{

    @Test
    public void longRunningServiceTest() throws Exception {
    }

}

메이븐 유닛 테스트 구성

이 솔루션의 장점은 사물의 단위 테스트 측면에서 실제로 변화가 없다는 것입니다.
우리는 통합 테스트를 무시하도록 구성을 maven surefire 플러그인에 추가하기 만하면됩니다.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.11</version>
    <configuration>
        <includes>
            <include>**/*.class</include>
        </includes>
        <excludedGroups>
            com.test.annotation.type.IntegrationTest
        </excludedGroups>
    </configuration>
</plugin>

를 수행하면 mvn clean test표시되지 않은 단위 테스트 만 실행됩니다.

Maven 통합 테스트 구성

다시 이것에 대한 구성은 매우 간단합니다.
표준 페일 세이프 플러그인을 사용하고 통합 테스트 만 실행하도록 구성합니다.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <includes>
            <include>**/*.class</include>
        </includes>
        <groups>
            com.test.annotation.type.IntegrationTest
        </groups>
    </configuration>
</plugin>

구성은 표준 실행 목표를 사용하여 빌드의 통합 테스트 단계에서 비상 안전 플러그인을 실행합니다.

이제 할 수 있습니다 mvn clean install.
이번에는 유닛 테스트와 통합 테스트가 통합 테스트 단계에서 실행됩니다.


답변

maven failsafe 플러그인을 사용해보십시오 . 특정 테스트 세트를 포함하도록 지시 할 수 있습니다.


답변

기본적으로 Maven은 클래스 이름에 Test가있는 테스트 만 실행합니다.

IntegrationTest로 이름을 바꾸면 아마도 작동 할 것입니다.

또는 Maven 구성을 변경하여 해당 파일을 포함시킬 수 있지만 테스트 이름을 SomethingTest로 지정하는 것이 더 쉽고 좋습니다.

에서 흠과 시험의 제외 :

기본적으로 Surefire 플러그인은 다음과 같은 와일드 카드 패턴을 가진 모든 테스트 클래스를 자동으로 포함합니다.

  • \*\*/Test\*.java – “Test”로 시작하는 모든 서브 디렉토리 및 모든 Java 파일 이름이 포함됩니다.
  • \*\*/\*Test.java -모든 하위 디렉토리와 “Test”로 끝나는 모든 Java 파일 이름이 포함됩니다.
  • \*\*/\*TestCase.java – “TestCase”로 끝나는 모든 서브 디렉토리 및 모든 Java 파일 이름이 포함됩니다.

테스트 클래스가 이름 지정 규칙을 따르지 않으면 Surefire 플러그인을 구성하고 포함 할 테스트를 지정하십시오.


답변

Maven과 통합 테스트를 실행하는 또 다른 방법은 프로파일 기능을 사용하는 것입니다.

...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
                <excludes>
                    <exclude>**/*IntegrationTest.java</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>integration-tests</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <includes>
                            <include>**/*IntegrationTest.java</include>
                        </includes>
                        <excludes>
                            <exclude>**/*StagingIntegrationTest.java</exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
...

실행 ‘새로 설치 MVN’ 기본 빌드를 실행합니다. 위에서 지정한대로 통합 테스트는 무시됩니다. ‘mvn clean install -P integration-tests’ 를 실행 하면 통합 테스트가 포함됩니다 (스테이징 통합 테스트도 무시합니다). 또한 매일 밤 통합 테스트를 실행하는 CI 서버가 있으며 ‘mvn test -P integration-tests’ 명령을 실행 합니다.