JUnit test with dynamic number of tests

前端 未结 7 652
灰色年华
灰色年华 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 18:58

    If TestNG is an option, you could use Parameters with DataProviders.

    Each individual file's test will have its result shown in the text-based report or Eclipse's TestNG plugin UI. The number of total tests run will count each of your files individually.

    This behavior differs from JUnit Theories, in which all results are lumped under one "theory" entry and only count as 1 test. If you want separate result reporting in JUnit, you can try Parameterized Tests.

    Test and inputs

    public class FileTest {
    
        @DataProvider(name="files")
        public File[][] getFiles(){
            return new File[][] {
                { new File("file1") },
                { new File("file2") }
            };
            // or scan a directory
        }
    
        @Test(dataProvider="files")
        public void testFile(File file){
            //run tests on file
        }
    }
    

    Example output

    PASSED: testFile(file1)
    PASSED: testFile(file2)
    
    ===============================================
        Default test
        Tests run: 2, Failures: 0, Skips: 0
    ===============================================
    

提交回复
热议问题