How to run entire JUnit Test Suite from within code

前端 未结 3 378
北荒
北荒 2021-01-13 18:19

In eclipse, with JUnit 4, you can right click a project or package and click Run as JUnit Test, and it will run all the JUnit tests within that grouping. Is there a way to d

3条回答
  •  花落未央
    2021-01-13 18:39

    You can use packages in junit such as JUnitCore like this:

    public static void main(String[] args){
        List tests = new ArrayList();
        tests.add(TestOne.class);
        tests.add(TestTwo.class);
    
        for (Class test : tests){
            runTests(test);
        }
    }
    
    private static void runTests(Class test){
        Result result = JUnitCore.runClasses(test);
        for (Failure failure : result.getFailures()){
            System.out.println(failure.toString());
        }
    }
    

提交回复
热议问题