Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit 4.11 parameterized and categorized methods

i have parameterized junit tests which i would like to group the test functions so as to run one group in one test suite and the other group in another.

What i have tried is this:

TestClass:

@RunWith(Parameterized.class)
public class TestClass {

    private int test;

    public TestClass(int test){
        this.test = test;
    }

    @Parameters
    public static Collection<Object[]> data(){
        return Arrays.asList(new Object[][]{{1},{1}});
    }

    @Test
    @Category(A.class)
    public void aTest(){
        assertEquals(1, test);
    }

    @Test
    @Category(B.class)
    public void bTest(){
        assertEquals(1, test);
    }

}

Test suite:

@SuiteClasses({TestClass.class})
@RunWith(Categories.class)
@IncludeCategory(A.class)
public class Suite {

}

If I annotate the test class, rather than the methods, it works. However I want to categorize the functions rather then the test class and when I try that I get the following error:

Category annotations on Parameterized classes are not supported on individual methods

How can I get this to work (without switching to TestNG or another testing framework)?

like image 741
pDiller Avatar asked Nov 12 '13 12:11

pDiller


1 Answers

You can create your own org.junit.runner.Runner to do that. It is not as hard as it looks. With your own runner you can build your own tree of test cases and JUnit will show it accordingly. In your case, your tree structure would reflect the category annotations.

like image 121
Akira Avatar answered Oct 03 '22 06:10

Akira