Parallel Execution / Session handling

In this session, we will discuss about the Parallel Execution / Session handling.

What you will do if you will face below problem.

  • If you have more Test to run in less time
  • If you need to run test for multiple browser at a time
  • If you need to run parallelly more than one test to check the stability of the application.

Yes. You can solve all these problem by implementing the parallel execution feature in TestNG.

Let’s discuss in detail

When you need to run more test in less time then you may think to optimize the test script which may not be a good approach because it will not optimize your execution time much as compared to Parallel execution. Also optimizing code will take time and more cost too.

In parallel execution you can execute multiple test parallel in which execution time will be less and you can run more test in stipulated time. Also, if you want to run cross browser testing then this is also one of the best way to achieve. For cross browser testing you need to use multiple browser as parameter and run parallel thought suite test. This will help user to execute the test methods / classes / tests parallelly.

In TestNG we can achieve parallel execution in several ways.

In testing.xml file , attribute parallel of suite can take below value.

  • test
  • method
  • class
  • instance

If we set parallel attribute with “test” then testing will run all @Test in the tag in the same thread but each tag will be inn separate thread.

In the same way, is we set parallel attribute with “method” then all methods will be running parallel. Same as class tag also

This parallel execution helps testng to execute parallelly in separate thread with respect to method, class and @Test.

Let’s understand this with an Example

Example:

Testcase file:
package testinglpoint;
import org.testng.annotations.Test;

public class TestParallelTesting {

	@Test
	public void testCase1() {
		System.out.println(Thread.currentThread().getId());
	}

	@Test
	public void testCase2() {
		System.out.println(Thread.currentThread().getId());
	}
}
Testng.xml file:
<suite name="Parallel test suite" parallel="methods" thread-count="2">
  <test name="Parallel">
    <classes>
      <class name="tesinglpoint.TestParallelTesting"/>
    </classes>
  </test>
</suite>
Output:

11

12

From the above example we understood the importance of Parallel Execution / Session handling

Leave a Reply

Your email address will not be published. Required fields are marked *