Skip to toolbar

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:


Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 9
  • Page views today : 24
  • Total visitors : 49,313
  • Total page view: 67,884

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.