2011-03-01 40 views
6

在我们的项目中,我们目前有大量(junit)测试,这些测试分为三类:单元,集成,检票。如何对大量JUnit测试进行分组/分类

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

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

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

回答

3

你可以把它们放在不同的包中。大多数IDE都可以运行给定包中的所有测试。在一个包中使用shell脚本在运行测试作为构建的一部分或其他内容时找到所有测试类也非常简单。我不知道是否有办法用蚂蚁做到这一点,但我会这么想。

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

你可能会滥用JUnit的假设机制来做你想做的事情:为每组测试设置一个系统属性,然后通过假设设置适当的属性来启动每个测试。运行所有测试将运行一切,但不想要的所有内容都将被忽略。

1

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

Grouping很简单。

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

或者,你可以创建3个ant脚本,每个脚本都会运行他们的单元测试,但这不太灵活。

+0

downvoter可以解释他们为什么要这样吗?使用TestNG是一个很好的建议。 – 2011-03-01 10:02:13

+0

同意。 TestNG已经足够成熟,可以成为公平的替代品。 – serg10 2011-03-01 10:52:42

+0

Tx Tom再次投票,我也想知道downvote背后的推理。 – 2011-03-01 11:57:07

2

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

正如所指出的其他评论者,这也正是为什么TestNG的使用字符串来定义组而不是注解:

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

一旦这种方式定义您的组,您可以包括和排除组使用正则表达式(例如“数据库。*”,“前端。*”等)。

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

尝试使用ClasspathSuite

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

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 
    } 
} 

接着样品被标记为在类级,这意味着运行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() { 
    } 
}