Sunday, 30 September 2018

How to achieve parallel execution in Test NG

In this blog, we will discuss and learn various options available in Test NG to execute in parallel.

Let us consider below sample Test NG xml:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="mySuite">
<test name="myTest">
<classes>
<class name="testNG.Test1"/>
<class name="testNG.Test2"/>
</classes>
</test>
</suite>

public class Test1 {

@Test
public void test1()
{
System.out.println("Thread ID ------------"+Thread.currentThread().getId());
System.out.println("test1");
}


}

public class Test2 {

@Test
public void test2()
{

System.out.println("Thread ID ------------"+Thread.currentThread().getId());
System.out.println("test2");

}

When we execute above suite, below is the output displayed. (output is in Blue)

[RemoteTestNG] detected TestNG version 6.14.2
Thread ID ------------1
test1
Thread ID ------------1
test2

===============================================
mySuite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Note here, for both the methods executed, Thread ID returned is one because, it ran tests in single thread (sequential execution).

Test NG provides options to run methods, tests, classes in different threads by using parallel attribute as given below:


<suite name="mySuite" parallel="methods" thread-count="2">
<suite name="mySuite" parallel="tests" thread-count="2">
<suite name="mySuite" parallel="classes" thread-count="2">
<suite name="mySuite" parallel="instances" thread-count="2">

parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.

parallel="tests": TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.

parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.

parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.


Let us modify our xml to be like this: 

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="mySuite" parallel="tests" thread-count="3">
<test name = "myTest">
<classes>
<class name="testNG.Test1"/>
</classes>
</test>
<test name = "myTest2">
<classes>
<class name="testNG.Test2"/>
</classes>
</test>
</suite>

Note changes: parallel= "tests" that means we want to execute our tests defined as myTest and myTest2 in parallel. and thread-count="3" totally there will be 3 threads available for use.

When I execute this test below is the output:

[RemoteTestNG] detected TestNG version 6.14.2
Thread ID ------------10
test2
Thread ID ------------9
test1

===============================================
mySuite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Output confirms that tests executed in parallel with different threads (thread ids).

Now, let us add one more method in Test2 class:

@Test
public void test3()
{

System.out.println("Thread ID ------------"+Thread.currentThread().getId());
System.out.println("test3");
}

Now let us execute same suite. Notice the output:

[RemoteTestNG] detected TestNG version 6.14.2
Thread ID ------------9
test1
Thread ID ------------10
test2
Thread ID ------------10
test3

===============================================
mySuite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

Since test2 and test3 are methods inside myTest2 Test, they got executed in same thread.

Now let us modify parallel ="methods" in above xml. It is expected to run each method in different thread. Below is the output which confirms the same.

[RemoteTestNG] detected TestNG version 6.14.2
Thread ID ------------9
test1
Thread ID ------------10
test2
Thread ID ------------11
test3

===============================================
mySuite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

Similarly parallel = "classes" can be used to run classes in parallel.

parallel="instances" would give below results:

[RemoteTestNG] detected TestNG version 6.14.2
Thread ID ------------9
test1
Thread ID ------------10
test2
Thread ID ------------10
test3

===============================================
mySuite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

Because, methods in same instance are run in same thread, whereas methods in other instance got run in different thread. 

Hope this blog has been useful to you.

Youtube channel: https://www.youtube.com/user/srinivaskinik
Facebook page: https://www.facebook.com/srinivaskinikalmady/








No comments:

Post a Comment

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule 1. Create a batch file with following content ...