77

JUnit 4.8と新しい@Categoryアノテーションを使用して、MavenのSurefireプラグインで実行するカテゴリのサブセットを選択する方法はありますか?

たとえば、私は持っています:

@Test
public void a() {
}

@Category(SlowTests.class)
@Test
public void b() {
}

そして、次のようにすべての非低速テストを実行したいと思います(-Dtest.categoriesは私が作成したことに注意してください...)。

mvn test -Dtest.categories=!SlowTests // run non-slow tests
mvn test -Dtest.categories=SlowTests // run only slow tests
mvn test -Dtest.categories=SlowTests,FastTests // run only slow tests and fast tests
mvn test // run all tests, including non-categorized

つまり、テストスイートを作成する必要はなく(Mavenはプロジェクト内のすべての単体テストを取得するだけで非常に便利です)、Mavenがカテゴリ別にテストを選択できるようにしたいということです。-Dtest.categoriesを作成したばかりだと思うので、同様の機能を使用できるかどうか疑問に思いました。

4

9 に答える 9

77

Maven はその後更新され、カテゴリを使用できるようになりました。

Surefire のドキュメントの例:

<plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.11</version>
      <configuration>
        <groups>com.mycompany.SlowTests</groups>
      </configuration>
</plugin>

これにより、注釈付きのクラスが実行されます@Category(com.mycompany.SlowTests.class)

于 2012-02-07T15:03:42.377 に答える
50

このブログ投稿に基づいて、単純化して、これを pom.xml に追加します。

<profiles>
    <profile>
        <id>SlowTests</id>
        <properties>
            <testcase.groups>com.example.SlowTests</testcase.groups>
        </properties>
    </profile>
    <profile>
        <id>FastTests</id>
        <properties>
            <testcase.groups>com.example.FastTests</testcase.groups>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.13</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.13</version>
                </dependency>
            </dependencies>
            <configuration>
                <groups>${testcase.groups}</groups>
            </configuration>
        </plugin>
    </plugins>
</build>

次にコマンドラインで

mvn install -P SlowTests
mvn install -P FastTests
mvn install -P FastTests,SlowTests
于 2013-08-18T09:14:43.937 に答える
1

まったく同じではありませんが、surefire プラグインを使用すると、ファイル名に基づいてテスト クラスを選択できます。ただし、Junit カテゴリを使用していません。

DAO テストのみを実行する例。

<executions>
  <execution>
     <id>test-dao</id>
        <phase>test</phase>
          <goals>
             <goal>test</goal>
        </goals>
          <configuration>
             <excludes>
                <exclude>none</exclude>
            </excludes>
            <includes>                  
                <include>**/com/proy/core/dao/**/*Test.java</include>
            </includes>
        </configuration>
  </execution>

http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html

于 2011-03-30T13:59:29.453 に答える