如何在JUnit 4中运行属于某个类别的所有测试 [英] How to run all tests belonging to a certain Category in JUnit 4
问题描述
JUnit 4.8包含一个名为Categories的新功能,允许您将某些类型的测试组合在一起。这非常有用,例如为慢速和快速测试进行单独的测试运行。我知道在 JUnit的4.8版本中提到的东西笔记,但想知道如何实际运行所有使用特定类别注释的测试。
JUnit 4.8 contains a nice new feature called "Categories" that allows you to group certain kinds of tests together. This is very useful, e.g. to have separate test runs for slow and fast tests. I know the stuff mentioned in JUnit 4.8 release notes, but would like to know how I can actually run all the tests annotated with certain category.
JUnit 4.8发行说明显示了一个示例套件定义,其中SuiteClasses注释选择某些类别的测试运行,如下所示:
The JUnit 4.8 release notes show an example suite definition, where SuiteClasses annotation selects the tests from certain category to run, like this:
@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b and B.c, but not A.a
}
有谁知道如何在SlowTests类别中运行所有测试?看来你必须有SuiteClasses注释...
Does anyone know how I could run all the tests in SlowTests category? It seems that you must have the SuiteClasses annotation...
推荐答案
我找到了一种可能的方法来实现我想要的,但是我不认为这是最好的解决方案,因为它依赖于不属于JUnit的ClassPathSuite库。
I found out one possible way to achieve what I want, but I don't consider this to be the best possible solution as it relies on ClassPathSuite library that is not part of JUnit.
我为这样的慢速测试定义测试套件:
I define the test suite for slow tests like this:
@RunWith(Categories.class)
@Categories.IncludeCategory(SlowTests.class)
@Suite.SuiteClasses( { AllTests.class })
public class SlowTestSuite {
}
AllTests中类这样定义:
AllTests class is defined like this:
@RunWith(ClasspathSuite.class)
public class AllTests {
}
我必须使用 ClassPathSuite 项目在这里。它会找到所有带有测试的类。
I had to use ClassPathSuite class from ClassPathSuite project here. It will find all the classes with tests.
这篇关于如何在JUnit 4中运行属于某个类别的所有测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!