To execute test methods in parallel and test classes in sequence using TestNG is discussed in this post.
Normally, TestNG supports parallel execution via testng.xml file in different ways like,
- tests – each of the <test> tags in the suite xml file would be executed in parallel.
- classes – all of the test classes would be executed in parallel
- methods – all of the @Test methods would be executed in parallel
- instances – in the case of factories that are powered by a data provider and when set would cause the instances produced by a factory to run in parallel
- false – This disables parallel execution behavior.
While trying all the above options, got a doubt that whether we can execute test methods in parallel and test classes in sequence using testNG.xml file.
Using above options, we cannot do it directly.
Does TestNG provide that option ? Yes… but how… Below is the solution provided for that.
In TestNG DTD, if we go through the @attr values, we can see an attribute called object-factory.
By definition, this attribute explains that “A class that implements IObjectFactory that will be used to instantiate the test objects”
Using this attribute object-factory, we can execute test methods in parallel and test classes in sequence using testNG.xml file.
Below is the working example and testng file.
- Testng.xml file:
If closely observed, we used object-factory attribute in the <suite> tag.
If we provide a full qualified package name of a class that implements the TestNG provided interface “org.testng.IObjectFactory2”, then TestNG will create our Test class instances only via our provided object-factory.
Now lets take a look at how the ObjectFactory Class looks like:
Created 4 test classes each having 2 test methods and here’s how one of the test classes in testng_parallel looks like.
Here’s the output looks like when we execute the testng.xml file with all those 4 classes getting executed within that package.
In this post we learned how to execute test methods in parallel and test classes in sequence using TestNG.