Maven-Surefire-Plugin을 사용하여 동일한 프로젝트에서 Junit 및 TestNG 테스트를 실행하는 방법은 무엇입니까?
-
22-07-2019 - |
문제
지금은 두 가지 유형의 테스트를 모두 가지고 있지만 "MVN 테스트"라고 말하면 junit이 아닌 TestNG 테스트 만 실행합니다. 나는 두 가지를 모두 실행하고 싶다. 아이디어가 있습니까?
해결책
있습니다 Thi에 대한 열린 문제S,이를위한 우아한 방법은 없습니다.
프레임 워크를 선택하고 고수하는 것이 훨씬 간단합니다.
편집 : 실행에 종속성을 지정할 수 없기 때문에 이전 답변이 작동하지 않습니다. 몇 가지 접근 방식을 시도했지만 관리 할 수있는 최선의 방법은 TestNG의 종속성에 대한 프로필을 작성하여 TestNG와 Junit 테스트 사이를 토글 할 수있는 것입니다. TestNG와 Junit 4 테스트를 모두 실행할 수있는 수단이없는 것 같습니다. .
주목해야 할 또 다른 점 : 할 수 있습니다 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>
이것에 대한 자세한 정보 : 하나의 Maven 모듈에서 TestNG 및 Junit 테스트 믹싱 - 2013 년판
이것에 대한 현재 링크 Maven-Surefire-Plugin 예제. "검색"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를위한 것입니다. 존재하지 않음을 지정하여 실행 당 테스트마다 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에게 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 플러그인을 다음과 같이 재정의 하여이 작업을 수행했습니다. 의존성은 확실히 주니트를 사용하도록합니다.
<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>
이전 솔루션을 기반으로합니다. 나는 이것이 우리에게 가장 잘 작동했다는 것을 알았습니다. 우리가 직면 한 한 가지 또 다른 문제는 오래된 주니트 테스트를 실행하려는 테스트였습니다. 우리는 모든 testng 테스트를 다르게 이름을 지정하여 이것을 피했습니다 (예 : *testng.java). 아래는 SureFire-Plugin의 두 가지 실행이있는 구성입니다.
<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 호환성에 대한 TestNG Asserts를 사용하는 것입니다.
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에게 이것은 내 문제를 해결했습니다
<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 Tests 및 TestNG 테스트를 모두 한 번만 실행합니다.
따라서 테스트 이름을 지정하는 데 제한이 없습니다.
플러그인 버전 :
SureFire-Plugin 2.16 (Junit47 및 TestNG 제공 업체 모두 2.16으로 설정)
Testng 종속성 6.8.7
주니의 종속성 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>
Just Run보다 : MVN Test -Pjunit -Tests (Junit 기반의 실행 테스트 용) 또는 MVN Test -Ptestng -Tests (TestNG Test 기반).