TestNG
provides an ability to run test methods, test classes and tests in parallel. By
using parallel execution, we can reduce the 'execution time' as tests are
started and executed simultaneously in different threads.We will create a class
with Two test methods and try to execute in different threads.
package com.parallel;
import org.testng.annotations.Test;
public
class TestParallelOne {
@Test
public
void
testCaseOne() {
//Printing Id of the thread on using which test method got executed
System.
out.println(
"Test Case One with Thread Id:- "
+ Thread.currentThread().getId());
}
@Test
public
void
testCaseTwo() {
////Printing Id of the thread on using which test method got executed
System.
out.println(
"Test Case two with Thread Id:- "
+ Thread.currentThread().getId());
}
}
The below is
the simple testng.xml file, if you observe, we are defining two attributes
'parallel' and 'thread-count' at suite level. As we want test methods to be
executed in parallel, we have provided 'methods'. And 'thread-count' attribute
is to used to pass the number of maximum threads to be created.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
<test name="Regression 1">
<classes>
<class name="com.parallel.TestParallelOne"/>
</classes>
</test>
</suite>
PARALLEL EXECUTION OF CLASSES IN TestNG
TestNG
provides an ability to run test classes in parallel. By using parallel
execution of classes, each class will be started and executed simultaneously in
different threads.
Let us look
at basic example for Parallel Execution of Classes using testng.xml.
We will
create a Two classes with Two test methods each and try to execute in different
threads.
Create class
and name it as : TestParallelClassOne.java
package com.parallel;
import org.testng.annotations.Test;
public
class TestParallelClassOne {
@Test
public
void
testCaseOne() {
// Printing class name and Id of the thread on using which test method got executed
System.out.println(
"Test Case One in " + getClass().getSimpleName()
+
" with Thread Id:- " + Thread.currentThread().getId());
}
@Test
public
void
testCaseTwo() {
//Printing class name and Id of the thread on using which test method got executed
System.out.println(
"Test Case two in " + getClass().getSimpleName()
+
" with Thread Id:- " + Thread.currentThread().getId());
}
}
Create
class and name it as : TestParallelClassTwo.java
package com.parallel;
import org.testng.annotations.Test;
public
class TestParallelClassTwo {
@Test
public
void
testCaseOne() {
//Printing class name and Id of the thread on using which test method got executed
System.out.println(
"Test Case One in " + getClass().getSimpleName()
+
" with Thread Id:- " + Thread.currentThread().getId());
}
@Test
public
void
testCaseTwo() {
//Printing class name and Id of the thread on using which test method got executed
System.out.println(
"Test Case Two in " + getClass().getSimpleName()
+
" with Thread Id:- " + Thread.currentThread().getId());
}
}
The
below is the testng.xml file
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="classes" thread-count="2">
<test name="Test 1">
<classes>
<class name="com.parallel.TestParallelClassOne"/>
<class name="com.parallel.TestParallelClassTwo"/>
</classes>
</test>
</suite>
In the above
testng.xml file, we have defined two attributes 'parallel' and 'thread-count'
at suite level. As we want classes to be executed in parallel, we have provided
'parallel="classes''. And 'thread-count' attribute is to used to pass the
number of maximum threads to be created.
No comments:
Post a Comment