How to run entire JUnit Test Suite from within code

前端 未结 3 380
北荒
北荒 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 19:01

    Use JUnit Suite:

    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
    
    @RunWith(Suite.class)
    // Put your Test Case Class here
    @Suite.SuiteClasses({
        JunitTest1.class,
        JunitTest2.class,
        JunitTest3.class
    })
    public class JunitTestSuite {}
    

    Then create a main method to run it.

    import org.junit.runner.JUnitCore;
    import org.junit.runner.Result;
    import org.junit.runner.notification.Failure;
    
    public class JunitTestSuiteRunner {
    
    public static void main(String[] args) {
    
        Result result = JUnitCore.runClasses(JunitTestSuite.class);
        for (Failure fail : result.getFailures()) {
            System.out.println(fail.toString());
        }
        if (result.wasSuccessful()) {
            System.out.println("All tests finished successfully...");
        }
    }}
    

提交回复
热议问题