Junit4:运行一套特定的测试方法

时间:2011-01-24 14:38:32

标签: testing junit suite

有没有办法创建一套测试方法,而不仅仅是测试类?

我想组建一个测试套件,它只运行测试类中的特定测试方法。我没有从我有限的junit知识和搜索网络中看到这样做的方法。

1 个答案:

答案 0 :(得分:7)

在JUnit4中使用类别功能。

示例:如果预期执行ATestBTest中分散的某些方法:

//Define Categories
@RunWith(Categories.class)  
@IncludeCategory(NeedTest.class)  
@SuiteClasses({ ATest.class,  BTest.class })  
class MySuite{
...
}

然后在ATestBTest中,将您的期望方法注释为:

@Test
@Category(NeedTest.class)  
public void test()

运行MySuite时,只会执行使用@Category(NeedTest.class)注释的方法。当然,您可以创建多个测试类别,

ps:NeedTest.class只是一个标记类,它可以是任何类。