Skip to main content
Introducing the Automate SDK! Get your entire test suite running on BrowserStack in minutes! Learn More.
No Result Found

Run JUnit Tests in Parallel

On BrowserStack, you can run multiple JUnit tests at the same time across various browser, device and OS combinations. This is “Parallel Testing”. Parallel Testing gives you the same benefits as running a multi-threaded application and helps you reduce the run time of your test suite, resulting in faster build times and faster releases.

To run tests on multiple browsers in parallel with JUnit on BrowserStack Automate, follow the below steps:

  1. Clone the junit-browserstack repo on GitHub(if not already done):

     git clone -b selenium-4 https://github.com/browserstack/junit-browserstack.git
     cd junit-browserstack/junit-4
    
  2. Install the dependencies using the following command:

     mvn install
    
  3. Update parallel.conf.json files within the junit-4/src/test/resources/conf/parallel.conf.json directory with your BrowserStack credentials as shown below:

    parallel.conf.json
     {
       "server": "hub-cloud.browserstack.com",
       "user": "YOUR_USERNAME",
       "key": "YOUR_ACCESS_KEY",
    
       "capabilities": {
         "browserstack.debug": true
       },
    
       "environments": [{
         "browser": "chrome"
       },{
         "browser": "firefox"
       },{
         "browser": "safari"
       },{
         "browser": "internet explorer"
       },{
         "device": "Samsung Galaxy S21"
       },{
         "device": "iPhone 12"
       }]
     }
    
  4. Capabilities for each environment can be customised as explained above. You need the following custom script to launch the tests in parallel.

    junit-browserstack/src/test/java/com/browserstack/Parallelized.java
     public class Parallelized extends Parameterized {
    
       private static class ThreadPoolScheduler implements RunnerScheduler {
         private ExecutorService executor;
    
         public ThreadPoolScheduler() {
           String threads = System.getProperty("junit.parallel.threads", "16");
           int numThreads = Integer.parseInt(threads);
           executor = Executors.newFixedThreadPool(numThreads);
         }
    
         @Override
         public void finished() {
           executor.shutdown();
           try {
             executor.awaitTermination(10, TimeUnit.MINUTES);
           } catch (InterruptedException exc) {
             throw new RuntimeException(exc);
           }
         }
    
         @Override
         public void schedule(Runnable childStatement) {
           executor.submit(childStatement);
         }
       }
    
       public Parallelized(Class klass) throws Throwable {
         super(klass);
         setScheduler(new ThreadPoolScheduler());
       }
     }
    
  5. You can now run your tests in parallel on BrowserStack using the following command:

     mvn test -P parallel
    
Note: Achieve your test coverage and build execution time goals by using our calculator to understand how many parallel sessions you need.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle