maven-surefire-pluginを使用して同じプロジェクトでJUnitおよびTestNGテストを実行する方法は?
-
22-07-2019 - |
質問
今は両方のタイプのテストがありますが、「mvn test」と言うとTestNGテストのみを実行し、Junitは実行しません。両方を次々に実行したい。どんなアイデア?
解決
未解決の問題があるため、エレガントな方法はありませんこれ。
フレームワークを選択してそれを使用する方がはるかに簡単です。
編集:実行時に依存関係を指定できないため、以前の回答は機能しません。 いくつかのアプローチを試しましたが、TestNGとJUnitのテストを切り替えることができるように、TestNG依存関係のプロファイルを作成することが最善です。TestNGとJunit 4の両方のテストを実行する手段はないようです。 。
注意すべきもう1つの点: TestNGからJUnitテストを起動できます、しかしこれはJUnit 3テストでのみ機能すると思います。
他のヒント
プロバイダーを選択する公式の方法。
複数のプロバイダーを依存関係として指定することもできます。これらのプロバイダーはすべて実行され、共通のレポートが生成されます。これは、含まれるプロバイダーを結合するためのユースケースがほとんどないため、外部プロバイダーでは特に便利です。
<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テストを混合&#8211; 2013年版
maven-surefire-pluginの例における現在のリンク。 &quot; Running TestNG and JUnit Tests &quot;を検索します。
次のように、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
の2つの実行を作成することです。1つはJUnit用、もう1つはTestNG用です。存在しない junitArtifactName
または testNGArtifactName
を指定することにより、実行ごとにTestNGまたはJUnitのいずれかを無効にできます。
<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に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>
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>
ビルドツールの構成を変更せずに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テストとtestngテストの両方を一度だけ実行します。
そのため、テストの命名に制限はありません。
プラグインのバージョン:
surefire-plugin 2.16(junit47およびtestngプロバイダーは両方ともバージョン2.16に設定)
testng依存関係6.8.7
junit依存関係4.7
<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テストに基づく)。