Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run entire JUnit Test Suite from within code

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 do this same thing from within the code?

like image 735
meanderingmoose Avatar asked Dec 07 '22 01:12

meanderingmoose


1 Answers

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());
    }
}
like image 78
Sean F Avatar answered Dec 31 '22 13:12

Sean F