2

我正在为我的一组测试用例创建自定义注释,我只想选择那些符合我的注释标准的方法并动态创建一个测试套件。

有没有办法将方法添加到测试套件而不是整个类?

就像是

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

4

2 回答 2

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(){}  

}
于 2012-05-17T14:58:26.463 回答
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(){ }
    }

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

如何将它们添加到测试套件中?您建议的显然是编译时解决方案。用户想要一个运行时解决方案。在给出标准之前,用户不知道哪些方法将成为测试套件的一部分。

我也在为 JUNIT4 寻找相同的解决方案。我想这在 Junit3 中是可能的。不过不确定。如果您遇到此用例的解决方案,请告诉我们。

于 2012-05-22T05:28:44.993 回答