How to parameterize junit Test Suite

后端 未结 7 1883
梦毁少年i
梦毁少年i 2021-01-14 06:42

Is it possible to parameterize a TestSuite in junit 4 ?

For declaring a class as a test suite I need the annotation @RunWith(Suite.class), but the same

7条回答
  •  借酒劲吻你
    2021-01-14 07:21

    As already stated multiple times, it's not possible to parameterize a test suite with the runners provided by JUnit 4.

    Anyway, I wouldn't recommend to make your testclasses dependent from some externally provided state. What if you want to run a single testclass?

    I would recommend to make your separate test classes @Parameterized and use a utility class to provide the parameters:

    @RunWith(Suite.class)
    @SuiteClasses({ Test1.class, Test2.class })
    public class TestSuite {
        // suite
    }
    
    @RunWith(Parameterized.class}
    public class Test1 {
        public Test1(Object param1) { /* ... */ }
    
        @Parameters
        public static Collection data() {
            return TestParameters.provideTestData()
        }
    
        @Test
        public void someTest() { /* ... */ }
    }
    
    @RunWith(Parameterized.class}
    public class Test2 {
        public Test2(Object param1) { /* ... */ }
    
        @Parameters
        public static Collection data() {
            return TestParameters.provideTestData()
        }
    
        @Test
        public void someOtherTest() { /* ... */ }
    }
    
    class TestParameters {
        public static Collection provideTestData() {
            Collection data = new ...;
            // build testdata
        return data;
    }
    

提交回复
热议问题