JUnit test with dynamic number of tests

前端 未结 7 650
灰色年华
灰色年华 2020-11-30 18:42

In our project I have several JUnit tests that e.g. take every file from a directory and run a test on it. If I implement a testEveryFileInDirectory method in t

7条回答
  •  日久生厌
    2020-11-30 19:03

    JUnit 3

    public class XTest extends TestCase {
    
        public File file;
    
        public XTest(File file) {
            super(file.toString());
            this.file = file;
        }
    
        public void testX() {
            fail("Failed: " + file);
        }
    
    }
    
    public class XTestSuite extends TestSuite {
    
        public static Test suite() {
            TestSuite suite = new TestSuite("XTestSuite");
            File[] files = new File(".").listFiles();
            for (File file : files) {
                suite.addTest(new XTest(file));
            }
            return suite;
        }
    
    }
    

    JUnit 4

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    @RunWith(Parameterized.class)
    public class TestY {
    
        @Parameters
        public static Collection getFiles() {
            Collection params = new ArrayList();
            for (File f : new File(".").listFiles()) {
                Object[] arr = new Object[] { f };
                params.add(arr);
            }
            return params;
        }
    
        private File file;
    
        public TestY(File file) {
            this.file = file;
        }
    
        @Test
        public void testY() {
            fail(file.toString());
        }
    
    }
    

提交回复
热议问题