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

查看:194
本文介绍了如何在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)

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

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