分组 JUnit 测试 [英] Grouping JUnit tests

查看:45
本文介绍了分组 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天全站免登陆