JUnit ParallelComputer Runs All Test Classes As One



  • I'm building automation testing for web application by using JUnit 4.9 and Selenium Webdriver. It's time consuming. So I'm trying to experiment with ParallelComputer.

    I'm wondering how can I get result of each test. In my current code below, it will run as one test and will pass even though there's an assertion error in one of the test classes. I'm new to the framework and parallel. Thanks!

    public class ParallelComputerTest 
    {
    
        @shizuka     public void test() 
        {
            Class[] cls={19 test classes in here};
    
            //Parallel among classes
            JUnitCore.runClasses(ParallelComputer.classes(), cls);
        }
    
    }
    


  • Inspired by JUnit's experimental ParallelComputer runner I've built my own ParallelSuite and ParallelParameterized runners. Using these runners one can easily parallelize test suites and parameterized tests.

    ParallelSuite.java

    public class ParallelSuite extends Suite {
        public ParallelSuite(Class klass, RunnerBuilder builder) throws InitializationError {
    
            super(klass, builder);
    
            setScheduler(new RunnerScheduler() {
    
                private final ExecutorService service = Executors.newFixedThreadPool(4);
    
                public void schedule(Runnable childStatement) {
                    service.submit(childStatement);
                }
    
                public void finished() {
                    try {
                        service.shutdown();
                        service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
                    } catch (InterruptedException e) {
                        e.printStackTrace(System.err);
                    }
                }
            });
        }
    }
    

    ParallelParameterized.java

    public class ParallelParameterized extends Parameterized {
    
        public ParallelParameterized(Class arg0) throws Throwable {
    
            super(arg0);
    
            setScheduler(new RunnerScheduler() {
    
                private final ExecutorService service = Executors.newFixedThreadPool(8);
    
                public void schedule(Runnable childStatement) {
                    service.submit(childStatement);
                }
    
                public void finished() {
                    try {
                        service.shutdown();
                        service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
                    } catch (InterruptedException e) {
                        e.printStackTrace(System.err);
                    }
                }
            });
        }
    }
    

    Usage is simple. Just change @RunWith annotations value to one of these Parallel* classes.

    @RunWith(ParallelSuite.class)
    @SuiteClasses({ATest.class, BTest.class, CTest.class})
    public class ABCSuite {}
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2