quas0r
quas0r

Reputation: 41

How can I add methods to the JUNIT test suite..?

I am creating custom annotations to my set of test cases and i'd like to pick up only those methods that match my annotation criteria and create a test suite on the fly.

Is there any way to add methods to test suite rather than the whole class?

Something like

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

Upvotes: 2

Views: 1460

Answers (2)

quantumcrypt
quantumcrypt

Reputation: 543

@Shengyuan, what the user wanted is like say

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

Now, using reflctions, my annotation processing class will return me a SET/LIST of methods which match my criteria (say,attrib1="foo"). Methid A and method C will satisfy. Now I need to add these to a test suite at runtime and run that.

How can I add them to the test suite? What u suggested is clearly compile time solution. The user wants a runtime solution. The user is unaware which methods will be a part of the test suite untill the criteria is given.

I too am searching for the same solution for JUNIT4. I guess this was possible in Junit3. Not sure though. Let us know if u come across a solution for this use case.

Upvotes: 0

卢声远 Shengyuan Lu
卢声远 Shengyuan Lu

Reputation: 32014

Use Categories feature.

Sample: Declare test suit:

interface Cate1{};

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

Test cases, only m1() will be run:

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

    @Test  
    public void m2(){}  

}

Upvotes: 3

Related Questions