java 如何对大量 JUnit 测试进行分组/分类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5152507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to group/categorize large number of JUnit tests
提问by maze2k
In our project, we currently have a large number of (junit) tests that are split into three categories: unit, integration, wicket.
在我们的项目中,我们目前有大量的(junit)测试,它们分为三类:单元、集成、检票口。
I now want to group these tests so I can run only one (or two) of those categories. The only thing I found are junit test suites and categories as described here: http://www.wakaleo.com/component/content/article/267
我现在想对这些测试进行分组,以便我只能运行这些类别中的一个(或两个)。我发现的唯一一件事是这里描述的 junit 测试套件和类别:http: //www.wakaleo.com/component/content/article/267
My problem is, I don't want to declare every single test in the Test Suits with @SuiteClasses.
我的问题是,我不想用@SuiteClasses 声明测试套装中的每一个测试。
Is there a way to add the suite classes with wildcards / patterns?
有没有办法用通配符/模式添加套件类?
回答by Vitali Tchalov
Assuming my understanding of the question is correct, it actually can be done using JUnit. The code below was used with JUnit 4.11 and allowed us to split all tests into 2 categories: "uncategorized" and Integration.
假设我对问题的理解是正确的,实际上可以使用 JUnit 来完成。下面的代码与 JUnit 4.11 一起使用,并允许我们将所有测试分为两类:“未分类”和集成。
IntegrationTestSuite.java
集成测试套件
/**
* A custom JUnit runner that executes all tests from the classpath that
* match the <code>ca.vtesc.portfolio.*Test</code> pattern
* and marked with <code>@Category(IntegrationTestCategory.class)</code>
* annotation.
*/
@RunWith(Categories.class)
@IncludeCategory(IntegrationTestCategory.class)
@Suite.SuiteClasses( { IntegrationTests.class })
public class IntegrationTestSuite {
}
@RunWith(ClasspathSuite.class)
@ClasspathSuite.ClassnameFilters({ "ca.vtesc.portfolio.*Test" })
class IntegrationTests {
}
UnitTestSuite.java
单元测试套件
/**
* A custom JUnit runner that executes all tests from the classpath that match
* <code>ca.vtesc.portfolio.*Test</code> pattern.
* <p>
* Classes and methods that are annotated with the
* <code>@Category(IntegrationTestCategory.class)</code> category are
* <strong>excluded</strong>.
*/
@RunWith(Categories.class)
@ExcludeCategory(IntegrationTestCategory.class)
@Suite.SuiteClasses( { UnitTests.class })
public class UnitTestSuite {
}
@RunWith(ClasspathSuite.class)
@ClasspathSuite.ClassnameFilters({ "ca.vtesc.portfolio.*Test" })
class UnitTests {
}
IntegrationTestCategory.java
IntegrationTestCategory.java
/**
* A marker interface for running integration tests.
*/
public interface IntegrationTestCategory {
}
The first sample test below is not annotated with any category so all its test methods will be included when running the UnitTestSuite and excluded when running IntegrationTestSuite.
下面的第一个示例测试没有使用任何类别进行注释,因此在运行 UnitTestSuite 时将包括其所有测试方法,并在运行 IntegrationTestSuite 时排除它。
public class OptionsServiceImplTest {
@Test
public void testOptionAssignment() {
// actual test code
}
}
Next sample is marked as Integration test on the class level which means both its test methods will be excluded when running the UnitTestSuite and included into IntegrationTestSuite:
下一个示例在类级别上标记为集成测试,这意味着在运行 UnitTestSuite 时将排除其测试方法并包含在 IntegrationTestSuite 中:
@Category(IntegrationTestCategory.class)
public class PortfolioServiceImplTest {
@Test
public void testTransfer() {
// actual test code
}
@Test
public void testQuote() {
}
}
And the third sample demos a test class with one method not annotated and the other marked with the Integration category.
第三个示例演示了一个测试类,其中一个方法没有注释,另一个标记为集成类别。
public class MarginServiceImplTest {
@Test
public void testPayment() {
}
@Test
@Category(IntegrationTestCategory.class)
public void testCall() {
}
}
回答by Rikash
Try using ClasspathSuite
尝试使用ClasspathSuite
I also had the same problem where I had more then 5500 jUnit tests. I categorised then into 3 groups and created 3 suites using the above jUnit extension. Its great.
我也有同样的问题,我有超过 5500 个 jUnit 测试。然后我将其分为 3 个组并使用上述 jUnit 扩展创建了 3 个套件。这很棒。
回答by Tom Anderson
You could put them in different packages. Most IDEs have a way to run all the tests in a given package. It's also pretty simple to find all the test classes in a package with a shell script for running tests as part of a build or whatever. I don't know if there's a way to do it with ant, but I would imagine so.
你可以把它们放在不同的包里。大多数 IDE 都有一种方法可以运行给定包中的所有测试。使用 shell 脚本在包中查找所有测试类也非常简单,用于在构建或其他过程中运行测试。我不知道是否有办法用蚂蚁来做到这一点,但我想是这样。
TestNG lets you tag tests as being in particular groups, then run those groups. That sound like exactly what you want, apart from the fact that it's not JUnit!
TestNG 允许您将测试标记为特定组,然后运行这些组。这听起来正是您想要的,除了它不是 JUnit!
You could abuse JUnit's assumption mechanism to do what you want: have a system property for each group of tests, and then start each test by assuming that the appropriate property is set. Running all tests will run everything, but everything you don't want will be ignored.
您可以滥用 JUnit 的假设机制来做您想做的事:为每组测试设置一个系统属性,然后通过假设设置了适当的属性来启动每个测试。运行所有测试将运行所有内容,但您不想要的所有内容都将被忽略。
回答by Cedric Beust
Even if you use JUnit categories, you still won't be able to use wildcards/patterns since categories are annotations, which are Java types.
即使您使用 JUnit 类别,您仍然无法使用通配符/模式,因为类别是注解,即 Java 类型。
As pointed out by other commenters, this is exactly why TestNG uses strings to define groups instead of annotations:
正如其他评论者所指出的,这正是 TestNG 使用字符串来定义组而不是注释的原因:
@Test(groups = { "database.ACCOUNTS", "fast-test" })
public void newAccountsShouldBeCreated() { ... }
Once you have defined your groups this way, you can include and exclude groups using regular expressions (e.g. "database.*", "front-end.*", etc...).
一旦您以这种方式定义了您的组,您就可以使用正则表达式(例如“database.*”、“front-end.*”等...)包括和排除组。
TestNG is indeed not based on JUnit, but it's very easy to convert all your JUnit tests to TestNG. Here are two blog posts that give an overview of the process:
TestNG 确实不是基于 JUnit,但是将所有 JUnit 测试转换为 TestNG 非常容易。以下是两篇博客文章,概述了该过程:
http://beust.com/weblog/2011/01/04/one-click-test-conversions/
http://beust.com/weblog/2011/01/04/one-click-test-conversions/
http://beust.com/weblog/2011/02/07/are-your-unit-tests-talking-to-each-other-behind-your-back/
http://beust.com/weblog/2011/02/07/are-your-unit-tests-talking-to-each-other-behind-your-back/
回答by Andrzej Jozwik
回答by Stijn Geukens
have you considered using TestNG? This is built on JUnit but a lot more powerfull: See comparison.
你考虑过使用TestNG吗?这是基于 JUnit 构建的,但功能更强大:请参阅比较。
Groupingis easy.
分组很容易。
Tranforming your tests from JUnit to TestNG should be straightforward.
将您的测试从 JUnit 转换为 TestNG 应该很简单。
Alternatively, you could create 3 ant scripts that will each run their unit tests but this is less flexible.
或者,您可以创建 3 个 ant 脚本,每个脚本都将运行其单元测试,但这不太灵活。