Maven에서 카테고리별로 JUnit 테스트를 실행하는 방법은 무엇입니까?
JUnit 4.8과 새로운 @Category
주석을 사용하여 Maven의 Surefire 플러그인으로 실행할 범주의 하위 집합을 선택하는 방법이 있습니까?
예를 들면 다음과 같습니다.
@Test
public void a() {
}
@Category(SlowTests.class)
@Test
public void b() {
}
그리고 다음과 같이 모든 비 느린 테스트를 실행하고 싶습니다. (-Dtest.categories는 저에 의해 구성되었습니다 ...).
mvn test -Dtest.categories=!SlowTests // run non-slow tests
mvn test -Dtest.categories=SlowTests // run only slow tests
mvn test -Dtest.categories=SlowTests,FastTests // run only slow tests and fast tests
mvn test // run all tests, including non-categorized
그래서 요점은 테스트 스위트를 만들 필요가 없다는 것입니다 (Maven은 프로젝트의 모든 단위 테스트를 선택하므로 매우 편리합니다). Maven이 카테고리별로 테스트를 선택할 수 있기를 바랍니다. -Dtest.categories를 방금 구성한 것 같아서 비슷한 기능을 사용할 수 있는지 궁금합니다.
Maven은 이후 업데이트되었으며 카테고리를 사용할 수 있습니다.
Surefire 문서 의 예 :
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<groups>com.mycompany.SlowTests</groups>
</configuration>
</plugin>
주석이있는 모든 클래스를 실행합니다. @Category(com.mycompany.SlowTests.class)
이 블로그 게시물 과 단순화를 기반으로 다음을 pom.xml에 추가하십시오.
<profiles>
<profile>
<id>SlowTests</id>
<properties>
<testcase.groups>com.example.SlowTests</testcase.groups>
</properties>
</profile>
<profile>
<id>FastTests</id>
<properties>
<testcase.groups>com.example.FastTests</testcase.groups>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
<configuration>
<groups>${testcase.groups}</groups>
</configuration>
</plugin>
</plugins>
</build>
그런 다음 명령 줄에서
mvn install -P SlowTests
mvn install -P FastTests
mvn install -P FastTests,SlowTests
주어진 범주를 제외한 모든 테스트를 실행하려는 비슷한 경우가있었습니다 (예를 들어, 수백 개의 레거시 분류되지 않은 테스트가 있고 각 테스트를 수정할 수 없거나 수정하고 싶지 않기 때문입니다).
maven surefire 플러그인을 사용하면 다음과 같은 카테고리를 제외 할 수 있습니다.
<profiles>
<profile>
<id>NonSlowTests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludedGroups>my.category.SlowTest</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
당신이 사용할 수있는
mvn test -Dgroups="com.myapp.FastTests, com.myapp.SlowTests"
그러나 maven surefire 플러그인을 올바르게 구성했는지 확인하십시오.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.12.2</version>
</dependency>
</dependencies>
</plugin>
https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html의 문서를 참조하십시오.
나는 junit의 잘못된 버전, 확실한 플러그인의 잘못된 버전 또는 적합하지 않은 조합을 사용하고 있다고 생각했기 때문에 "groups / excludedGroups는 프로젝트 테스트 클래스 경로에 TestNG 또는 JUnit48 +가 필요합니다"라는 오류에 많은 시간을 잃었습니다.
It was none of that: in my project I had a "config" module that was built before the module I wanted to test. This module had no junit dependency -> it had no junit on the classpath...
This mistake may help others...
Not exactly the same thing but using surefire plugin, test classes can be chosen based on file name. You are not using Junit Categories though.
An example for running just DAO tests.
<executions>
<execution>
<id>test-dao</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/com/proy/core/dao/**/*Test.java</include>
</includes>
</configuration>
</execution>
http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html
참고URL : https://stackoverflow.com/questions/3100924/how-to-run-junit-tests-by-category-in-maven
'Programing' 카테고리의 다른 글
정밀도 수준이 다른 Date 객체 비교 (0) | 2020.11.04 |
---|---|
SQL * Plus를 사용하여 Oracle 11g에서 데이터베이스를 표시하는 방법 (0) | 2020.11.04 |
C ++ 크로스 플랫폼 고해상도 타이머 (0) | 2020.11.03 |
Putty에서 Windows로 파일을 복사하려면 어떻게해야합니까? (0) | 2020.11.03 |
스크롤 할 때 메뉴 막대를 상단에 고정 (0) | 2020.11.03 |