現在、両方のタイプのテストがありますが、「mvn テスト」と言うと、Junit ではなく TestNG テストのみを実行します。両方を次々と実行したい。何か案が ?
12 に答える
プロバイダーを選択する公式の方法。
複数のプロバイダーを依存関係として指定することもできます。それらはすべて実行され、共通のレポートが生成されます。含まれているプロバイダーを組み合わせるユースケースはほとんどないため、これは外部プロバイダーの場合に特に便利です。
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.18.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.18.1</version>
</dependency>
</dependencies>
</plugin>
これに関する詳細: 1 つの Maven モジュールでの TestNG と JUnit テストの混合 – 2013 年版
maven-surefire-plugin examples のこれに対する現在のリンク。「 TestNG および JUnit テストの実行」を検索します。
次のように、junit テストを無視するように testng プロバイダーを構成する必要があります。
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<properties>
<property>
<name>junit</name>
<value>false</value>
</property>
</properties>
</configuration>
[...providers as dependecies, see above...]
</plugin>
より良い解決策があります。
アイデアはmaven-surefire-plugin
、JUnit 用と TestNG 用の 2 つの実行を作成することです。nonexisting または を指定することで、実行ごとに TestNG または JUnit のいずれかを無効にすることができjunitArtifactName
ますtestNGArtifactName
。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
</configuration>
</execution>
<execution>
<id>test-testng</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<junitArtifactName>none:none</junitArtifactName>
</configuration>
</execution>
</executions>
</plugin>
これには未解決の問題があるため、これを行うエレガントな方法はありません。
フレームワークを選択してそれに固執する方がはるかに簡単です。
編集:実行時に依存関係を指定できないため、以前の回答は機能しません。私はいくつかのアプローチを試しましたが、私が管理できる最善の方法は、TestNG 依存関係のプロファイルを作成して、TestNG と JUnit のテストを切り替えることができるようにすることです。TestNG と JUnit 4 の両方のテストを実行する手段はないようです。 .
もう 1 つの注意点: JUnit テストは TestNG から起動できますが、これは JUnit 3 テストでのみ機能すると思います。
これには別の方法があります。TestNG に Junit テスト ケースを実行するように依頼することもできます。以下は、すべてのテスト ケースを実行するサンプルの testng.xml です。
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestAll">
<test name="junitTestCases" junit="true">
<packages>
<package name="com.test.*" />
</packages>
</test>
<test name="testNGTestCases" >
<packages>
<package name="com.test.*" />
</packages>
</test>
</suite>
このリンク ( http://jira.codehaus.org/browse/SUREFIRE-377 ) のおかげで、以前の問題 (2 回ではなく 3 回の実行) の解決策がここにあります。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<junitArtifactName>none:none</junitArtifactName>
<testNGArtifactName>org.testng:testng</testNGArtifactName>
</configuration>
</execution>
</executions>
</plugin>
ビルド ツールの構成を変更せずに TestNG で両方のテスト タイプを実行するソリューションを見つけました。
Gradle でテストしましたが、Maven でも動作するはずです。
これにより、TestNG 内で JUnit テストが実行されますが、逆方向には実行されないことに注意してください。
トリックは、テスト クラスで両方のフレームワークの注釈を使用し、JUnit 互換性のために TestNG アサートを使用することです。
import static org.testng.AssertJUnit.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@org.testng.annotations.Test
public final class ApplicationTest {
@org.testng.annotations.BeforeClass
@BeforeClass
public static void setup () {}
@org.testng.annotations.AfterClass
@AfterClass
public static void cleanup () {}
@Test public void json () throws IOException {
assertTrue (true);
}
}
このハックを使用すると、TestNG で既存の JUnit テストを簡単に実行できるため、時間が許すときにそれらを移行するのに役立ちます。
それが役に立てば幸い!
JUnit の場合 ---
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19.1</version>
</dependency>
</dependencies>
同様に、必要に応じて TestNG の依存関係を使用します
解決策は、確実なプラグインにJUnitを使用させることであることがわかりました。これを行うには、特定のプロジェクトでsurefireプラグインを次のようにオーバーライドします。依存関係により、surefireはJUnitを使用するように強制されます。
<build>
<plugins>
<!-- force sure fire to use junit instead of testng -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.10</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
以前のソリューションに基づいています。これが私たちにとって最もうまくいくことがわかりました。私たちが直面していたもう 1 つの問題は、TestNG が古い JUnit テストを実行しようとしていたことです。これを回避するには、すべての TestNG テストに異なる名前を付けました (例: *TestNG.java)。以下は、surefire-plugin を 2 回実行した構成です。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
<excludes>
<exclude>**/*TestNG.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>test-testng</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<junitArtifactName>none:none</junitArtifactName>
<testNGArtifactName>org.testng:testng</testNGArtifactName>
<includes>
<include>**/*TestNG.java</include>
</includes>
<excludes>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- ********* Skip Test for Success BUILD ******** -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!-- *********************************************** -->
</plugins>
</build>
<profiles>
<!-- ********** Profiles for run test cases ************ -->
<!-- Profile for run JUnit test dependent tests -->
<profile>
<id>junit-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<skip>false</skip>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.10</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
<!-- Profile for run TestNG dependent tests -->
<profile>
<id>testNG-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- ***************************************************** -->
</profiles>
実行するだけではありません: mvn test -Pjunit-tests (junit ベースの実行テストの場合) または mvn test -PtestNG-tests (TestNG テスト ベースの場合)。