187

私はmaven2マルチモジュールプロジェクトを持っており、各子モジュールには、それぞれ単体テストと統合テスト用のTest.java名前が付いたJUnitテストがあります。Integration.java私が実行すると:

mvn test

*Test.java子モジュール内のすべての JUnit テストが実行されます。実行すると

mvn test -Dtest=**/*Integration

Integration.java子モジュール内で実行されるテストはありません。

これらは私にはまったく同じコマンドのように見えますが、-Dtest= /*Integration** を使用したコマンドは機能せず、親レベルで実行されているテストが 0 と表示され、テストはありません。

4

10 に答える 10

273

Maven ビルド ライフサイクルには、統合テストを実行するための「統合テスト」フェーズが含まれるようになりました。これは、「テスト」フェーズで実行される単体テストとは別に実行されます。「package」の後に実行されるので、「mvn verify」、「mvn install」、または「mvn deploy」を実行すると、途中で統合テストが実行されます。

デフォルトでは、 integration-test は**/IT*.java**/*IT.java、およびという名前のテスト クラスを実行**/*ITCase.javaしますが、これは構成可能です。

これをすべて接続する方法の詳細については、Failsafe プラグインFailsafe の使用ページ(これを書いているので、前のページから正しくリンクされていません) を参照してください。また、この Sonatype ブログ投稿も参照してください。

于 2011-02-23T11:34:23.333 に答える
121

Maven の Surefire をセットアップして、単体テストと統合テストを別々に実行できます。標準単体テスト フェーズでは、パターン マッチしないすべての統合テストを実行します。次に、統合テストのみを実行する 2 番目のテスト フェーズを作成します。

次に例を示します。

    <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>
于 2009-09-09T12:04:55.393 に答える
70

私はあなたがやりたいことを正確に行いました、そしてそれは素晴らしい働きをします。単体テスト「*Tests」は常に実行され、「* IntegrationTests」は、mvnverifyまたはmvninstallを実行した場合にのみ実行されます。これが私の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>

幸運を!

于 2010-07-09T02:59:11.057 に答える
34

JUnit カテゴリと Maven を使用すると、非常に簡単に分割できます。
これは、単体テストと統合テストを分割することによって、以下に非常に簡単に示されています。

マーカー インターフェイスを定義する

カテゴリを使用してテストをグループ化する最初のステップは、マーカー インターフェイスを作成することです。
このインターフェイスは、統合テストとして実行するすべてのテストをマークするために使用されます。

public interface IntegrationTest {}

テスト クラスをマークする

テスト クラスの先頭にカテゴリ アノテーションを追加します。新しいインターフェースの名前を取ります。

import org.junit.experimental.categories.Category;

@Category(IntegrationTest.class)
public class ExampleIntegrationTest{

    @Test
    public void longRunningServiceTest() throws Exception {
    }

}

Maven 単体テストの構成

このソリューションの優れた点は、単体テストの面では何も変わらないことです。
統合テストを無視するように、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
今回は、実行中の単体テストと同様に、統合テスト フェーズで統合テストが実行されます。

于 2012-04-30T09:56:25.080 に答える
17

maven failsafe pluginを使用してみてください。特定の一連のテストを含めるように指示できます。

于 2010-05-18T18:29:11.133 に答える
16

デフォルトでは、Maven はクラス名のどこかに Test が含まれるテストのみを実行します。

名前を IntegrationTest に変更すると、おそらく機能します。

または、そのファイルを含めるように Maven 構成を変更することもできますが、テストに SomethingTest という名前を付ける方がおそらく簡単で優れています。

テストの包含と除外から:

デフォルトでは、Surefire プラグインは、次のワイルドカード パターンを持つすべてのテスト クラスを自動的に含めます。

  • \*\*/Test\*.java- そのすべてのサブディレクトリと、「Test」で始まるすべての Java ファイル名が含まれます。
  • \*\*/\*Test.java- そのすべてのサブディレクトリと、「Test」で終わるすべての Java ファイル名が含まれます。
  • \*\*/\*TestCase.java- そのすべてのサブディレクトリと、「TestCase」で終わるすべての Java ファイル名が含まれます。

テスト クラスが命名規則に従っていない場合は、Surefire プラグインを構成し、含めるテストを指定します。

于 2009-09-09T11:58:35.683 に答える
10

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 clean install'を実行すると、デフォルトのビルドが実行されます。上記のように、統合テストは無視されます。'mvn clean install -P Integration-tests'を実行すると、統合テストが含まれます(ステージング統合テストも無視します)。さらに、統合テストを毎晩実行するCIサーバーがあり、そのためにコマンド「mvn test-Pintegration-tests」を発行します。

于 2011-06-02T21:04:10.233 に答える