Execute test methods in parallel and test classes in sequence using TestNG - Explore Selenium

Explore Selenium

Selenium Automation

TestNG

Execute test methods in parallel and test classes in sequence using TestNG

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:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Class1Suite" parallel="methods" group-by-instances="true" object-factory="testng_parallel.ObjectFactory">
        <test name="instances">
            <packages>
                <package name="testng_parallel.*"/>
            </packages>
        </test>
    </suite> 
    

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:

  • package testng_parallel;
    import org.testng.IObjectFactory2;	 
    
    public class ObjectFactory implements IObjectFactory2 {
    	
    	    public ObjectFactory() {
    	        System.out.println("Custom Factory being created");
    	    }
    	 
    	    @Override
    	    public Object newInstance(Class<?> cls) {
    	        try {
    	            return cls.newInstance();
    	        } catch (InstantiationException | IllegalAccessException e) {
    	            e.printStackTrace();
    	        }
    	        return null;
    	    }
    	}
    

Created 4 test classes each having 2 test methods and here’s how one of the test classes in testng_parallel looks like.

  • package testng_parallel;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.Reporter;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    
    public class ClassOne {
        
    	public static WebDriver driver;
    	
    	 @BeforeClass
    	  public void createDriver() {
    		  System.setProperty("webdriver.chrome.driver", "F:\\Selenium_Drivers\\chromedriver_2.28.exe");
    			 driver = new ChromeDriver();
    	  }
    
    	  @AfterClass
    	  public void quitDriver() {
    	    driver.quit();
    	  }
    
    	  @Test
    	  public void testGoogleSearch1() {
    		  String method = Reporter.getCurrentTestResult().getMethod().getMethodName();
    	        System.out.println("Running " + getClass().getName() + "." + method + "() on Thread [" + Thread.currentThread()
    	            .getId() + "]");
    	    driver.get("http://www.google.com");
    	    System.out.println("Class ONE");
    	  }
    	
    	  @Test
    	  public void testGoogleSearch2() {
    		  String method = Reporter.getCurrentTestResult().getMethod().getMethodName();
    	        System.out.println("Running " + getClass().getName() + "." + method + "() on Thread [" + Thread.currentThread()
    	            .getId() + "]");
    	    driver.get("http://www.google.com");
    	    System.out.println("Class ONE");
    	  }
    }
    

Here’s the output looks like when we execute the testng.xml file with all those 4 classes getting executed within that package.

Console Output:

Summary:

In this post we learned how to execute test methods in parallel and test classes in sequence using TestNG.

Basic Usage of TestNG
0

1 Comment

  1. zvodretiluret

    Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

Leave a Reply