[英]How to run JUnit tests by category in Maven?
使用 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
我在这个错误“groups/excludedGroups 需要 TestNG 或 JUnit48+ on project test classpath”上浪费了很多时间,因为我认为我使用的是 junit 的错误版本,或者surefire插件的错误版本,或者不适合的组合。
不是这样:在我的项目中,我有一个“配置”模块,它是在我想要测试的模块之前构建的。 这个模块没有junit依赖->它在类路径上没有junit...
这个错误可能会帮助其他人......
不完全相同,但使用surefire插件,可以根据文件名选择测试类。 不过,您没有使用 Junit 类别。
仅运行 DAO 测试的示例。
<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
对于默认情况下要忽略某些测试然后有条件地从命令行运行所有测试的用例,此设置将适用于JUnit 4.9
。
首先创建标记界面:
public interface IntegrationTest {}
注释要排除的测试:
@Category(IntegrationTest.class)
public class MyIntegrationTest() {}
将插件和配置文件添加到pom.xml
:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<!-- Read JUnit categories to be excluded from Maven property -->
<configuration>
<excludedGroups>${test.excluded.groups}</excludedGroups>
</configuration>
</plugin>
</plugins>
<profiles>
<profile>
<id>Default</id>
<!-- Set profile to be active by default -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<test.excluded.groups>com.example.IntegrationTest</test.excluded.groups>
</properties>
</profile>
<profile>
<id>AllTest</id>
<!-- Set groups property to blank value which will match nothing -->
<properties>
<test.excluded.groups></test.excluded.groups>
</properties>
</profile>
</profiles>
从命令行照常运行测试时,将排除集成测试:
mvn test
可以使用相应的配置文件激活包括集成测试在内的所有测试:
mvn test -P AllTest
在通过mvn test -Dgroups=com.mycompany.SlowTests.class
运行测试时,我在运行带有类别的测试时遇到问题: @Category(com.mycompany.SlowTests.class)
我发现名称中没有Test
一词的类中的Test
将无法运行。 将单词Test
添加到类后,类中的测试运行。
Junit 5 允许您使用@Tag 注释。 有关更多信息,请访问: https : //www.baeldung.com/junit-filtering-tests
我发现它看起来更干净一点:
@Tag("SlowTests")
@Test
public void b() {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.