对JUnit测试进行分组 [英] Grouping JUnit tests

查看:362
本文介绍了对JUnit测试进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在JUnit中对测试进行分组,这样我才能只运行一些组?

Is there any way to group tests in JUnit, so that I can run only some groups?

或者可以注释一些测试,然后全局禁用它们?

Or is it possible to annotate some tests and then globally disable them?

我正在使用 JUnit 4 ,我无法使用TestNG。

I'm using JUnit 4, I can't use TestNG.

编辑: @RunWith和@SuiteClasses效果很好。但有可能只在测试类中注释一些测试?或者我是否需要注释整个测试类?

edit: @RunWith and @SuiteClasses works great. But is it possible to annotate like this only some tests in test class? Or do I have to annotate whole test class?

推荐答案

您是否要在测试类中对测试进行分组,或者您是否希望小组测试课程?我将假设后者。

Do you want to group tests inside a test class or do you want to group test classes? I am going to assume the latter.

这取决于你如何运行测试。如果您通过Maven运行它们,则可以准确指定要包含的测试。请参阅 Maven surefire文档

It depends on how you are running your tests. If you run them by Maven, it is possible to specify exactly what tests you want to include. See the Maven surefire documentation for this.

但更一般地说,我所做的是我有一个测试套件树。 JUnit 4中的测试套件类似于:

More generally, though, what I do is that I have a tree of test suites. A test suite in JUnit 4 looks something like:

 @RunWith(Suite.class)
 @SuiteClasses(SomeUnitTest1.class, SomeUnitTest2.class)
 public class UnitTestsSuite {
 }

所以,也许我有一个FunctionTestsSuite和一个UnitTestsSuite,然后是一个包含另外两个的AllTestsSuite。如果你在Eclipse中运行它们,你会得到一个非常好的层次结构视图。

So, maybe I have a FunctionTestsSuite and a UnitTestsSuite, and then an AllTestsSuite which includes the other two. If you run them in Eclipse you get a very nice hierarchical view.

这种方法的问题在于,如果你想在多个切片中进行切片,那就太乏味了不同的方式。但它仍然可能(例如,您可以根据模块切换一组套件,然后根据测试类型进行切片)。

The problem with this approach is that it's kind of tedious if you want to slice tests in more than one different way. But it's still possible (you can for example have one set of suites that slice based on module, then another slicing on the type of test).

这篇关于对JUnit测试进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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