TestNG – 20 || Run TestNG Program using main() method.

Running a TestNG Program using main() is very simple. We just need to use the object of TestNG class in main() and all set.

  • Till now we have used below steps to run a TestNG program:
    • Create class files and XML file
    • Rightclick on XML file
    • Select Run As
    • Click on TestNG Suite

If you want to learn how to create TestSuites using XML file please visit the link given below:

But in real life, there may be scenarios where we have to run these programs using main() methods as described in the below image:

TestNG Program using main().

TestNG Program using main()

We just need to add a few codes of lines into the main() method to achieve this. Here we need to use TestNG Class, as this is the entry class for the execution of the program.

  • Create an object of TestNG class.
  • Create another variable of List type using String data type.
  • Add the correct path of the TestSuite(target XML) file in the above list.
  • You can add path of multiple test suites in the above list.
  • Embed above path to the TestNG Object using setTestSuites(List<String>).
  • The last step is to call the run() method of the TestNG object. This method invokes the XML file.
  • Now, we have to just run this main() method and the program will execute successfully.

Code Example

Here, we are taking a simple example of a TestNG program. The program structure is mentioned for the reference.

  • BrowserHandling.java
    • @BeforeSuite – launchBrowser()
    • @AfterSuite – logout()
  • SampleTests.java
    • @Test – login()
  • UsingMainMethod.xml
    • <class name=”common.BrowserHandling”/>
    • <class name=”common.SampleTests”/>
  • RunProgram.java
    • main(String[] args)
      • testng.run()

BrowserHandling.java

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

/**
 * @author ashok.kumar
 *
 */
public class BrowserHandling 
{
	@BeforeSuite
	public void launchBrowser()
	{
		System.out.println("Browser and URL launched successfully...");
	}
	
	@AfterSuite
	public void logout()
	{
		System.out.println("User logged-out successfully...");
	}
}

SampleTests.java

import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 *
 */
public class SampleTests 
{
	@Test
	public void login()
	{
		System.out.println("User Logged in...");
	}
}

UsingMainMethod.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Using Main Method">
	<test name="SampleTest">
		<classes>
			<class name="UsingMainMethod.BrowserHandling"/>
			<class name="UsingMainMethod.SampleTests"/>
		</classes>
	</test>
</suite>

RunProgram.java

import java.util.List;
import org.testng.TestNG;
import com.beust.jcommander.internal.Lists;

/**
 * @author ashok.kumar
 *
 */
public class RunProgram {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
        {
	System.out.println("Execution of TestNG XML file using main() method.");
		TestNG testng=new TestNG();
		List<String> suite=Lists.newArrayList();
		suite.add(".\\src\\UsingMainMethod.xml");
		testng.setTestSuites(suite);
		testng.run();
	}
}

Console Output

Console Output.

TestNG:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


688total visits,1visits today

Leave a Reply

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