Maven과 함께 한 프로젝트에서 Junit4와 Testng
문제
함께 실행하기 위해 사용 가능한 옵션이 거의 없지만 Junit 및 TestNG에 대해 다른 프로파일을 사용하여 선택했습니다. 그러나 이제 문제는 테스트 사례를 제외하고 포함하는 것입니다.
Maven의 메인 프로젝트에 TestNG 의존성을 추가하면 모든 Junit을 건너 뛸 수 있습니다. 별도의 프로필에 넣기로 결정했습니다. 따라서 POM.XML에서 다음 항목을 사용하여 컴파일에서 기본 (기본) 프로파일에서 TestNG 테스트를 제외하고 있습니다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testExcludes>
<testExclude>**/tests/**.*</testExclude>
<testExclude>**/tests/utils/**.*</testExclude>
</testExcludes>
</configuration>
</plugin>
SureFire 플러그인에 대해서도 마찬가지입니다. 따라서 메인 프로필에서 잘 작동하며 Junit4 테스트 만 실행합니다. 그러나 testng 프로파일을 사용하면 testng 테스트를 실행하지 않아도 컴파일하지 않습니다. 다음 프로필을 사용하여 실행 중입니다.
<profile>
<id>testNG</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testIncludes>
<testInclude>**/tests/**.java</testInclude>
<testInclude>**/tests/utils/**.*</testInclude>
</testIncludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<includes>
<include>**/**.class</include>
<include>**/tests/utils/**.class</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.8</version>
<scope>test</scope>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
</profile>
왜 그것이 왜 그들을 포함시키고 다시 컴파일하지 않는지 아는 사람이 있습니까?
해결책
컴파일러 플러그인의 구성은 TestNG 유형을 제외합니다. 프로파일의 구성은 기본 구성과 병합되므로 효과적인 컴파일러 구성은 다음과 같습니다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testIncludes>
<testInclude>**/tests/**.java</testInclude>
<testInclude>**/tests/utils/**.*</testInclude>
</testIncludes>
<testExcludes>
<testExclude>**/tests/**.*</testExclude>
<testExclude>**/tests/utils/**.*</testExclude>
</testExcludes>
</configuration>
</plugin>
즉, TestNG 유형이 컴파일되지 않았으므로 실행되지 않음을 의미합니다.
당신이 지정하는 경우u003Cexcludes> testng 프로파일의 섹션은 기본값 제외를 무시하고 TestNG 유형이 컴파일되어 실행됩니다. 빈 제외 태그로 작동하는지 기억이 나지 않습니다 (예 :u003Cexcludes/> ), 기본 구성이 재정의되도록 이와 같은 것을 지정해야 할 수도 있습니다.
<testExcludes>
<testExclude>dummy</testExclude>
</testExcludes>
다른 팁
가장 간단한 솔루션은 다음과 같습니다
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
이것에 대한 자세한 정보 : 하나의 Maven 모듈에서 TestNG 및 Junit 테스트 믹싱 - 2013 년판
SureFire와 FailSafe를 위해 온라인으로 결합 된 솔루션을 찾지 못했습니다. 아래의 POM 파일 변경은 저에게 효과적이었습니다.
- SureFire 트릭은 Junit 및 TestNG의 하위 플러그인을 명시 적으로 지정하여 수행됩니다.
- Failsafe 트릭은 각각 빈 구성이있는 두 개의 실행을 정의하여 수행됩니다.
두 솔루션 모두 온라인에서 찾은 해킹입니다. 확실한 트릭에 대한 링크는이 질문에 대한 또 다른 답변이라고 생각합니다.
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.version}</version>
<executions>
<execution>
<id>test-testng</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
</configuration>
</execution>
<execution>
<id>test-junit</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<junitArtifactName>none:none</junitArtifactName>
</configuration>
</execution>
</executions>
</plugin>
<!-- ... -->
</plugins>