起動中は、1つのテストのみをスキップしたいと思いmvn install
ます。
それを行う方法はありますか?
junit 4では、それ@Ignore
を実行したいときに注釈を追加します。これは、テストをたまに無視する場合、またはビルドがMavenから実行される場合にのみ無視する場合を除いて、機能します。もしそうなら、私は「なぜ?」と尋ねるでしょう。
テストは一貫している必要があり、移植可能であり、常に合格する必要があります。特定のテストに問題がある場合は、それを書き直すか、完全に削除するか、別のテストスイートまたはプロジェクトに移動することを検討します。
統合テストを除外する必要があるのは通常ですが、単体テストを含める必要があります。これを実現するには、すべての統合テストに接尾辞IntegrationTest(AbcIntegrationTest.javaなど)を付けて名前を付けることをお勧めします。
次に、Mavenビルドに次のように入力します。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</plugin>
これを使用してビルドすると、すべての統合テストが除外されますが、他のすべてのテスト(単体テストなど)が実行されます。完全 :-)
テスト実行中のテストの除外と包含の詳細については、以下をお読みください。
http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
PS 1つのテストを除外するには、除外リストで明示的に名前を付ける必要があります。簡単。
CLIを使用して1つのテストを除外する場合は、-Dtest
および-Dit.test
フラグを使用する必要があります。
デフォルトをリセットするように注意してください。表記を使用している場合、!
すべてのデフォルトが消去されるため、元に戻す必要があります。によって実行される通常のテストのsurefire
場合はを追加する必要がありますが*Test, Test*, *Tests, *TestCase
、によって実行される統合テストのfailsafe
場合はを追加する必要がありますIT*, *IT, *ITCase
。
詳細については、https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html(通常のテスト)を参照してください。
そしてここhttps://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html(統合テスト)
-Dit.test='!ITsometestIT, IT*, *IT, *ITCase'
完全なmvn
コマンドは次のようになります。
mvn -e -B -Dtest='!unitTestABC, *Test, Test*, *Tests, *TestCase' -Dit.test='!ITintegrationTestABCIT, IT*, *IT, *ITCase' -DfailIfNoTests=false clean install
'
使用することを忘れないでください"
。二重引用符を使用する場合、!
それらの内部はすべてによって評価されbash
ます。
統合テストは、の場合は実行されないことにも注意してくださいmvn test
。統合テストのみが実行され、mvn verify
単体テストは実行されません
アノテーションを使用して、このソリューションをご覧ください@Category
public class AccountTest {
@Test
@Category(IntegrationTests.class)
public void thisTestWillTakeSomeTime() {
...
}
@Test
@Category(IntegrationTests.class)
public void thisTestWillTakeEvenLonger() {
...
}
@Test
public void thisOneIsRealFast() {
...
}
}
次に、テストスイートを使用して実行します。
@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { AccountTest.class, ClientTest.class })
public class LongRunningTestSuite {}
たとえば、mavenを使用してこれらのテストを含める( groups
)/除外する( )こともできます。excludedGroups
mvn -DexcludedGroups=com.mycompany.tests.IntegrationTests test
このコマンドを使用すると、これが機能するはずです。
mvn archetype:create -DgroupId=test -DartifactId=test
(テストの場合、pom.xmlとtest-classを次のように変更し、mvn installを使用します)
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>
test/AppTest.java
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
</dependencies>
テストクラス:
package test;
import org.junit.Test;
import static org.junit.Assert.fail;
public class AppTest
{
@Test
public void test_it() {
fail("not implemented");
}
}