Monday, 16 March 2020

TestNG: Run test classes parallel

You can specify number of parallel threads that execute given suite of test classes in suite definition xml file.

Example
<suite name="Parallel Classes" parallel="classes" thread-count="2">

</suite>

Find the below working application.

ParallelClassDemo1.java
package com.sample.app.tests;

import org.testng.annotations.Test;

public class ParallelClassDemo1 {
  @Test
  public void a() {
    System.out.println("ParallelClassDemo1_a is executed by : " + Thread.currentThread().getName());
  }

  @Test
  public void b() {
    System.out.println("ParallelClassDemo1_b is executed by : " + Thread.currentThread().getName());
  }

  @Test
  public void c() {
    System.out.println("ParallelClassDemo1_c is executed by : " + Thread.currentThread().getName());
  }

  @Test
  public void d() {
    System.out.println("ParallelClassDemo1_d is executed by : " + Thread.currentThread().getName());
  }

  @Test
  public void e() {
    System.out.println("ParallelClassDemo1_e is executed by : " + Thread.currentThread().getName());
  }
}

ParallelClassDemo2.java
package com.sample.app.tests;

import org.testng.annotations.Test;

public class ParallelClassDemo2 {
  @Test
  public void a() {
    System.out.println("ParallelClassDemo2_a is executed by : " + Thread.currentThread().getName());
  }

  @Test
  public void b() {
    System.out.println("ParallelClassDemo2_b is executed by : " + Thread.currentThread().getName());
  }

  @Test
  public void c() {
    System.out.println("ParallelClassDemo2_c is executed by : " + Thread.currentThread().getName());
  }

  @Test
  public void d() {
    System.out.println("ParallelClassDemo2_d is executed by : " + Thread.currentThread().getName());
  }

  @Test
  public void e() {
    System.out.println("ParallelClassDemo2_e is executed by : " + Thread.currentThread().getName());
  }
}

parallelClasses.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >

<suite name="Parallel Classes" parallel="classes" thread-count="2">

  <test name="ParallelClassesDemo">
    <classes>
      <class name="com.sample.app.tests.ParallelClassDemo1"></class>
      <class name="com.sample.app.tests.ParallelClassDemo2"></class>
    </classes>
  </test>

</suite>

How to run the suite?
Right click on ‘parallelClasses.xml’ -> Run As -> TestNG Suite.

You will get below messages in console.

[RemoteTestNG] detected TestNG version 7.0.0
ParallelClassDemo1_a is executed by : TestNG-test=ParallelClassesDemo-1
ParallelClassDemo2_a is executed by : TestNG-test=ParallelClassesDemo-2
ParallelClassDemo1_b is executed by : TestNG-test=ParallelClassesDemo-1
ParallelClassDemo2_b is executed by : TestNG-test=ParallelClassesDemo-2
ParallelClassDemo1_c is executed by : TestNG-test=ParallelClassesDemo-1
ParallelClassDemo2_c is executed by : TestNG-test=ParallelClassesDemo-2
ParallelClassDemo1_d is executed by : TestNG-test=ParallelClassesDemo-1
ParallelClassDemo2_d is executed by : TestNG-test=ParallelClassesDemo-2
ParallelClassDemo1_e is executed by : TestNG-test=ParallelClassesDemo-1
ParallelClassDemo2_e is executed by : TestNG-test=ParallelClassesDemo-2

===============================================
Parallel Classes
Total tests run: 10, Passes: 10, Failures: 0, Skips: 0
===============================================


Previous                                                    Next                                                    Home

No comments:

Post a Comment