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

时间:2011-03-01 09:18:25

标签: java junit

在我们的项目中,我们目前有大量(junit)测试,分为三类:单元,集成,检票口。

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

我的问题是,我不想在@SuiteClasses的测试套装中声明每一个测试。

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

6 个答案:

答案 0 :(得分:7)

假设我对这个问题的理解是正确的,实际上可以使用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

/**
 * A marker interface for running integration tests.
 */
public interface IntegrationTestCategory {
}

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

public class OptionsServiceImplTest {
    @Test
    public void testOptionAssignment() {
        // actual test code
    }
}

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

@Category(IntegrationTestCategory.class)
public class PortfolioServiceImplTest {
    @Test
    public void testTransfer() {
        // actual test code
    }
    @Test
    public void testQuote() {
    }
}

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

public class MarginServiceImplTest {
    @Test
    public void testPayment() {
    }
    @Test
    @Category(IntegrationTestCategory.class)
    public void testCall() {
    }
}

答案 1 :(得分:4)

尝试使用ClasspathSuite

我也有同样的问题,我有超过5500次jUnit测试。我将其分为3组,并使用上面的jUnit扩展创建了3个套件。很棒。

答案 2 :(得分:3)

您可以将它们放在不同的包中。大多数IDE都有办法在给定的包中运行所有测试。在包中使用shell脚本查找所有测试类也非常简单,这些脚本用于将测试作为构建的一部分运行。我不知道是否有办法用蚂蚁来做,但我想是这样。

TestNG允许您将测试标记为特定组,然后运行这些组。这听起来就像你想要的,除了它不是JUnit!

您可以滥用JUnit的假设机制来执行您想要的操作:为每组测试设置一个系统属性,然后通过假设设置了相应的属性来启动每个测试。运行所有测试将运行所有测试,但您不想要的所有内容都将被忽略。

答案 3 :(得分:2)

即使您使用JUnit类别,您仍然无法使用通配符/模式,因为类别是注释,它们是Java类型。

正如其他评论者所指出的,这正是TestNG使用字符串来定义组而不是注释的原因:

@Test(groups = { "database.ACCOUNTS", "fast-test" })
public void newAccountsShouldBeCreated() { ... }

以这种方式定义组后,您可以使用正则表达式包含和排除组(例如“database。*”,“front-end。*”等等。)

TestNG确实不是基于JUnit,但将所有JUnit测试转换为TestNG非常容易。以下是两篇博文,概述了该过程:

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/

答案 4 :(得分:2)

答案 5 :(得分:1)

你考虑过使用TestNG吗? 这是建立在JUnit上的,但功能更强大:请参阅comparison

Grouping很简单。

将测试从JUnit转换为TestNG应该很简单。

或者,您可以创建3个蚂蚁脚本,每个脚本都运行其单元测试,但这不太灵活。

相关问题