How to parameterize junit Test Suite

后端 未结 7 1885
梦毁少年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:26

    I was able to parameterize a test suite and use its data in a test class member of the suite as follows:

    In JUTsuite:

    @RunWith(Suite.class)
    @Suite.SuiteClasses({ 
        JUT_test1.class,
    })
    
    public class JUTSuite{  
        // Declare all variables/objects you want to share with the test classes, e.g.
        protected static List<Fx> globalFxs;
        // This is the data list we'll use as parameters
        protected static List<Dx> globalDxs;
    
        @Parameters
        public static Collection<Object[]> data(){
            // Instantiate object list for parameters.  
            // Note: you must do it here and not in, say, @BeforeClass setup()
            // e.g.
            globalDxs=new ArrayList<Dx>(serverObj.values());
    
            Collection<Object[]> rows=new ArrayList<Object[]>();
            for(Dx d:globalDxs) {
                rows.add(new Object[]{d});
            }
            return rows;
        }
    
        @BeforeClass
        public static void setUp() throws Exception {
            // Instantiate/initialize all suite variables/objects to be shares with test classes
            // e.g. globalFxs=new ArrayList<Fx>();
        }
    
        @AfterClass
        public static void tearDown() throws Exception {
            // Clean up....
        }
    }
    

    Next, in test class:

    @RunWith(Parameterized.class)
    public class JUT_test1 {
        // declare local names (if desired) for suite-wide variable/objects 
        // e.g. 
        private static List<Fx> globalFxs;
    
        // This is the test parameter:      
        private Dx d;
    
        public JUT_test1(Dx d){
            this.d=d;
        }
    
        @Parameters
        public static Collection<Object[]> data(){
        // Note: we're calling the suite's data() method which has already executed.
            return JUTSuite.data();
        }
    
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
        // (If desired)initialize local variables by referencing suite variables.
        // e.g.globalFxs=JUTSuite.globalFxs;
        }
    }
    
    0 讨论(0)
提交回复
热议问题