java – 如何对大量JUnit测试进行分组/分类

前端之家收集整理的这篇文章主要介绍了java – 如何对大量JUnit测试进行分组/分类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我们的项目中,我们目前有大量(junit)测试,分为三类:单元,集成,检票口.

我现在想要对这些测试进行分组,以便我只能运行其中一个(或两个)类别.我发现的唯一的东西是junit测试套件和类别,如下所述:http://www.wakaleo.com/component/content/article/267

我的问题是,我不想用@SuiteClasses在Test Suits中声明每一个测试.

有没有办法添加带有通配符/模式的套件类?

解决方法

假设我对这个问题的理解是正确的,实际上可以使用JUnit来完成.下面的代码与JUnit 4.11一起使用,并允许我们将所有测试分为两类:“未分类”和集成.

IntegrationTestSuite.java

  1. /**
  2. * A custom JUnit runner that executes all tests from the classpath that
  3. * match the <code>ca.vtesc.portfolio.*Test</code> pattern
  4. * and marked with <code>@Category(IntegrationTestCategory.class)</code>
  5. * annotation.
  6. */
  7. @RunWith(Categories.class)
  8. @IncludeCategory(IntegrationTestCategory.class)
  9. @Suite.SuiteClasses( { IntegrationTests.class })
  10. public class IntegrationTestSuite {
  11. }
  12.  
  13. @RunWith(ClasspathSuite.class)
  14. @ClasspathSuite.ClassnameFilters({ "ca.vtesc.portfolio.*Test" })
  15. class IntegrationTests {
  16. }

UnitTestSuite.java

  1. /**
  2. * A custom JUnit runner that executes all tests from the classpath that match
  3. * <code>ca.vtesc.portfolio.*Test</code> pattern.
  4. * <p>
  5. * Classes and methods that are annotated with the
  6. * <code>@Category(IntegrationTestCategory.class)</code> category are
  7. * <strong>excluded</strong>.
  8. */
  9.  
  10. @RunWith(Categories.class)
  11. @ExcludeCategory(IntegrationTestCategory.class)
  12. @Suite.SuiteClasses( { UnitTests.class })
  13. public class UnitTestSuite {
  14. }
  15.  
  16. @RunWith(ClasspathSuite.class)
  17. @ClasspathSuite.ClassnameFilters({ "ca.vtesc.portfolio.*Test" })
  18. class UnitTests {
  19. }

IntegrationTestCategory.java

  1. /**
  2. * A marker interface for running integration tests.
  3. */
  4. public interface IntegrationTestCategory {
  5. }

下面的第一个示例测试未使用任何类别进行注释,因此在运行UnitTestSuite时将包括其所有测试方法,并在运行IntegrationTestSuite时将其排除.

  1. public class OptionsServiceImplTest {
  2. @Test
  3. public void testOptionAssignment() {
  4. // actual test code
  5. }
  6. }

下一个示例在类级别上标记为Integration测试,这意味着在运行UnitTestSuite并将其包含在IntegrationTestSuite中时,将排除其测试方法

  1. @Category(IntegrationTestCategory.class)
  2. public class PortfolioServiceImplTest {
  3. @Test
  4. public void testTransfer() {
  5. // actual test code
  6. }
  7. @Test
  8. public void testQuote() {
  9. }
  10. }

第三个示例演示了一个测试类,其中一个方法没有注释,另一个标记为Integration类.

  1. public class MarginServiceImplTest {
  2. @Test
  3. public void testPayment() {
  4. }
  5. @Test
  6. @Category(IntegrationTestCategory.class)
  7. public void testCall() {
  8. }
  9. }

猜你在找的Java相关文章