如何在 Maven 中按类别运行 JUnit 测试? [英] How to run JUnit tests by category in Maven?

查看:41
本文介绍了如何在 Maven 中按类别运行 JUnit 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 JUnit 4.8 和新的 @Category 注释,有没有办法选择类别的子集以与 Maven 的 Surefire 插件一起运行?

Using JUnit 4.8 and the new @Category annotations, is there a way to choose a subset of categories to run with Maven's Surefire plugin?

例如我有:

@Test
public void a() {
}

@Category(SlowTests.class)
@Test
public void b() {
}

而且我想运行所有非慢速测试,如:(注意 -Dtest.categories 是由我组成的......).

And I'd like to run all non-slow tests as in: (note that the -Dtest.categories was made up by me...).

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,所以我想知道是否有类似的工具我可以使用?

So the point is that I don't want to have to create test suites (Maven just picks up all unit tests in the project which is very convenient) and I'd like Maven to be able to pick the tests by category. I think I just made up the -Dtest.categories, so I was wondering if there's a similar facility I can use?

推荐答案

Maven 已经更新并且可以使用类别.

Maven has since been updated and can use categories.

Surefire 文档中的示例:

<plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.11</version>
      <configuration>
        <groups>com.mycompany.SlowTests</groups>
      </configuration>
</plugin>

这将运行带有注释的任何类 @Category(com.mycompany.SlowTests.class)

This will run any class with the annotation @Category(com.mycompany.SlowTests.class)

这篇关于如何在 Maven 中按类别运行 JUnit 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆