Parallel Testing in TestNG

  • Estimate Completion Time
    not found 30 minutes
  • Difficulty Level:

Write three TestNG test methods and run them parallelly to observe that the total time taken should be less than the sum of the sleep times.

Steps to follow

Create 3 test methods with different wait times (1 sec, 10 secs, and 5 sec) using Thread.sleep(time in milliseconds).

Run as TestNG test and observe the time which is the total of sleep time added i.e. 16 seconds. Now in the TestNG XML file, use the parallel attribute to run these methods in parallel mode and observe if the time taken is less than the total time or the maximum time i.e. 10 seconds.

Solution

package practice;
import org.testng.annotations.Test;
public class ParallelTest {

	@Test
	public void testMethod1() {
Thread.sleep(1000);
	System.out.println("Sleep time is 1 sec");		
//write code
	}
	@Test
	public void testMethod2() {
	Thread.sleep(10000);
	System.out.println("Sleep time is 10 sec");		
//write code
	}
@Test
	public void testMethod3() {
	Thread.sleep(5000);
	System.out.println("Sleep time is 5 sec");
	//write code
	}
}
<suite name="ParallelSuite" parallel="methods" thread-count="3">
    <test name="ParallelTest">
        <classes>
            <class name="practice.ParallelTest"/>
        </classes>
    </test>
</suite>
TestNG Practice Exercises - QA Ads Banner

Proleed Academy

Proleed serves / offers professionally designed IT training courses
globally.

Copyright © 2023 - Proleed Academy | All Rights Reserved.