programing

Maven Integration 테스트를 실행하려면 어떻게 해야 합니까?

kingscode 2022. 9. 2. 00:04
반응형

Maven Integration 테스트를 실행하려면 어떻게 해야 합니까?

있으며 에는 maven2라는 .Test.java ★★★★★★★★★★★★★★★★★」Integration.java유닛 테스트와 통합 테스트에 각각 사용됩니다.시 : 행행: :

mvn test

"JUnit"*Test.java하위 모듈 내에서 실행됩니다.★★★★★★★★★★★★★★를 실행했을 때,

mvn test -Dtest=**/*Integration

도 아니다Integration.java이치노

동일한 명령어 같지만 -Dtest=/*를 사용하는 명령어입니다.Integration**는 기능하지 않고 부모 레벨에서 실행되고 있는0개의 테스트를 표시합니다.이 테스트에는 테스트가 없습니다.

현재 Maven 빌드 라이프 사이클에는 통합 테스트를 실행하기 위한 "통합 테스트" 단계가 포함되어 있습니다.이 단계는 "테스트" 단계에서 실행되는 유닛 테스트와는 별도로 실행됩니다.패키지 후에 실행되므로 "mvn verify", "mvn install" 또는 "mvn deploy"를 실행하면 통합 테스트가 진행됩니다.

는 integration-test라는 이름의 합니다.**/IT*.java,**/*IT.java , , , , 입니다.**/*ITCase.java를 설정할 수 있습니다.

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

Maven's 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>

난 당신이 원하는 것을 정확히 해왔고 그것은 잘 작동한다.유닛 테스트 "*Tests" 및 "*Integration"은 항상 실행됩니다.Tests"는 mvn verify 또는 mvn 설치를 수행할 때만 실행됩니다.여기 내 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 。
이것은 유닛 분할 및 통합 테스트에 의해 아래에 매우 간략하게 나타나 있습니다.

마커 인터페이스 정의

The first step in grouping a test using categories is to create a marker interface.
This interface will be used to mark all of the tests that you want to be run as integration tests.

public interface IntegrationTest {}

테스트 클래스에 표시

테스트 클래스의 맨 위에 카테고리 주석을 추가합니다.새로운 인터페이스의 이름을 사용합니다.

import org.junit.experimental.categories.Category;

@Category(IntegrationTest.class)
public class ExampleIntegrationTest{

    @Test
    public void longRunningServiceTest() throws Exception {
    }

}

메이븐 유닛 테스트 설정

The beauty of this solution is that nothing really changes for the unit test side of things.
We simply add some configuration to the maven surefire plugin to make it to ignore any integration tests.

<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 연동 테스트 설정

Again the configuration for this is very simple.
We use the standard failsafe plugin and configure it to only run the integration tests.

<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 surefire 플러그인을 사용하고 통합 테스트를 실행하려면 maven fail safe 플러그인을 사용해야 합니다.

플래그를 사용하여 이러한 테스트의 실행을 전환하려면 아래를 따르십시오.

메이븐 설정

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>${skipUnitTests}</skipTests>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*IT.java</include>
                </includes>
                <skipTests>${skipIntegrationTests}</skipTests>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <properties>
            <skipTests>false</skipTests>
            <skipUnitTests>${skipTests}</skipUnitTests>
            <skipIntegrationTests>${skipTests}</skipIntegrationTests>
        </properties>

따라서 테스트는 다음 플래그 규칙에 따라 건너뛰거나 전환됩니다.

테스트는 다음 플래그를 사용하여 건너뛸 수 있습니다.

  • -DskipTests 건너뜁니다.
  • -DskipUnitTests합니다.
  • -DskipIntegrationTests합니다.

테스트 실행

유닛 테스트만 실행하려면 아래를 실행합니다.

mvn clean test

다음 명령을 실행하여 테스트를 실행할 수 있습니다(유닛과 통합 모두).

mvn clean verify

연동 테스트만 실행하려면 다음 절차를 따릅니다.

mvn failsafe:integration-test

또는 유닛 테스트를 건너뜁니다.

mvn clean install -DskipUnitTests

, 「」중에서의 도,mvn install, 로,

mvn clean install -DskipIntegrationTests

다음을 사용하여 모든 테스트를 건너뛸 수 있습니다.

mvn clean install -DskipTests

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

기본적으로 Maven은 클래스 이름의 어딘가에 검정이 있는 검정만 실행합니다.

통합으로 이름 변경테스트해 보면 아마 효과가 있을 거야

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

테스트의 포함제외 항목:

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

  • \*\*/Test\*.java하는 모든 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 연동 테스트' 명령을 발행합니다.

메이븐 설명서에 따라 빌드 시 유닛테스트를 실행하고 통합테스트를 개별적으로 실행할 수 있습니다.

<project>
    <properties>
        <skipTests>true</skipTests>
    </properties>
    [...]
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <skipITs>${skipTests}</skipITs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    [...]
</project>

이를 통해 기본적으로 모든 연동 테스트를 비활성화할 수 있습니다.이 명령어를 실행하려면 다음 명령을 사용합니다.

mvn install -DskipTests=false

다른 답변에 제시된 모든 설정을 수행할 필요가 없으며 유닛 테스트 또는 통합 테스트를 개별적으로 실행할 수 있습니다.

내가 한 일은:

  1. pom.xml에 config bellow를 추가하여 통합 테스트 파일(Java 코드 및 리소스 파일)의 위치를 지정합니다.
<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>add-integration-test-source</id>
                <phase>generate-test-sources</phase>
                <goals>
                    <goal>add-test-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>src/integration-test/java</source>
                    </sources>
                </configuration>
            </execution>
            <execution>
                <id>add-integration-test-resource</id>
                <phase>generate-test-resources</phase>
                <goals>
                    <goal>add-test-resource</goal>
                </goals>
                <configuration>
                    <resources>
                        <resource>
                            <directory>src/integration-test/resources</directory>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
  1. 통합 테스트 파일의 이름을 "로 지정합니다."MyTest"와 같은 IT 서픽스IT.java"

  2. 다음 명령을 사용하여 통합 테스트만 수행합니다.

    mvn failsafe:integration-test failsafe:verify

위의 명령어 "failsafe:integration-test"에서는 *IT.java 파일이 실행되며 "failsafe:verify"에서는 이전에 실행된 통합 테스트가 실패했는지 확인하라는 메시지가 나타납니다.'failsafe:verify'를 사용하지 않으면 테스트 하나가 실패하더라도 mvn 명령이 성공적으로 종료됩니다.

  1. 장치 테스트만 실행하려면 다음을 수행합니다.

    mvn test

언급URL : https://stackoverflow.com/questions/1399240/how-do-i-get-my-maven-integration-tests-to-run

반응형