Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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 
{

    @Test
    public void test() 
    {
        Class[] cls={19 test classes in here};

        //Parallel among classes
        JUnitCore.runClasses(ParallelComputer.classes(), cls);
    }

}
share|improve this question
add comment

2 Answers 2

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 {}
share|improve this answer
add comment

The test is marked as passed because you're are invoking JUnitCore from within a test class. As soon as JUnitCore.runClasses has completed execution, the current test, ie 'test' in 'ParallelComputerTest', is finished succesfully since no AssertionError were thrown during that test, and that was a single test as far as your IDE's test runner is concerned.

Now, you can achieve your requirement like so:

public class TestRunner{

  public static void main(String[] args){
    Class[] cls = { TestCase1.class, TestCase2.class, ..[as many test classes as you want] };
    Result result = JUnitCore.runClasses(ParallelComputer.classes(), cls);
    List<Failure> failures = result.getFailures();     
  }   
}

and then invoke the test like so :

TestRunner.main(args);

The list failures will have a bunch of Failure objects which you can parse and log. The Result object will have the overall test details, viz. execution time, tests started, ignore, finished and failed.

Please note that ParallelComputer also has other methods apart from classes() which will allow you to have a somewhat higher degree of control over what is executed in parallel.

share|improve this answer
    
Thanks for your answer. I was able to use result and failure to log. Where should I invoke it and how do I run it? –  Peijie Mar 7 at 17:35
    
The TestRunner class can be the start of your execution assuming you are running the test from command line. Any configuration that you might need can be passed as the arguments to this. –  Ashwin Mar 28 at 10:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.