2012-05-16 58 views
2

我正在为我的测试用例集创建自定义注释,我只想拾取符合我的注释条件的那些方法,并即时创建一个测试套件。如何将方法添加到JUNIT测试套件中?

有什么办法可以添加方法到测试套件而不是整个类?

喜欢的东西

@Suite.Suitemethods({ Support.method1(), Test2.method2(), ServiceName.method3(), ServiceName2.method4() })

回答

3

使用分类功能

样品: 申报测试服:

interface Cate1{}; 

@RunWith(Categories.class) 
@IncludeCategory(Cate1.class)//Only allow @Category(Cate1.class) 
@SuiteClasses({Support.class, 
       Test2.class, 
       ServiceName.class,...}) 
class MyTest{} 

测试的情况下,只有M1()将运行:

class Support{ 
    @Test 
    @Category(Cate1.class) 
    public void m1(){} 

    @Test 
    public void m2(){} 

} 
0

@Shengyuan,什么用户想要的是像说

Class A{ 

    @Test  
    @CustomAnnotation(attrib1 = "foo"; attrib2 = "moo"; attrib3 = "poo") 
    void methodA(){ } 

    @Test  
    @CustomAnnotation(attrib1 = "blahblah"; attrib2 = "flahflah"; attrib3 = "klahklah") 
    void methodB(){ } 

    @Test  
    @CustomAnnotation(attrib1 = "foo"; attrib2 = "flahflah"; attrib3 = "poo") 
    void methodC(){ } 
    } 

现在,使用reflctions,我的注释处理类将返回一个方法的SET/LIST列表whi ch符合我的标准(比如attrib1 =“foo”)。方法A和方法C将满足。现在我需要在运行时将这些添加到测试套件并运行它。

如何将它们添加到测试套件? 你建议的是明确的编译时解决方案。 用户需要运行时解决方案。用户不知道哪些方法将成为测试套件的一部分,直到给出标准。

我也在为JUNIT4寻找相同的解决方案。 我想这在Junit3中是可能的。但不知道。让我们知道,如果你遇到这个用例的解决方案。