Running TestNG Tests Programmatically
Running testng tests programmatically will discuss about how to execute testng tests programmatically. Till now we have executed all the tests using testng.xml file in the previous blogs. In this blog, will see how to avoid xml file (i.e testng.xml) and execute the same functionality using java file(i.e. programmatically).
We have some limitations with the xml file that xml files are static and can not be changed at runtime. As per the requirement sometimes we need to change some of the configurations at runtime, which is depends on an Excel sheet or database data interaction. To achieve these kinds of things TestNG provided a feature to define and execute TestNG tests through program by using some of the APIs provided by the TestNG.
All the things which is possible with the testng.xml file can be achieved by the API provided by TestNG. And after completion of writing the program we can easily execute the code as this is a normal java file.
Below are the two sample programs we can execute these files using testng program.
First Sample java file:
import org.testng.Assert; import org.testng.annotations.Test; public class SampleProgramOne { @Test public void testOne() { Assert.assertTrue(true); System.out.println("Pass Test case - testOne"); } @Test public void testTwo() { Assert.assertEquals("AutomationgTesting", "AutomationgTest"); System.out.println("Pass Test case - testOne"); } @Test public void testThree() { Assert.assertTrue(true); System.out.println("Pass Test case - testThree"); } }
Second Sample java file:
import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class SampleProgramTwo { @BeforeMethod public void beforeMethod() { System.out.println("I am in beforeMethod"); } @Test public void testOne() { System.out.println("I am in Simple Test"); } @AfterMethod public void afterMethod() { System.out.println("I am in afterMethod"); } }
Using Listeners is mandatory while executing the suite through code. Otherwise it will not show the errors information in detail. Means, which method got failed and what is exact error message will not be given if you not using the listeners. Below is the sample listener class.
import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; public class ListenerTest implements ITestListener { @Override public void onTestStart(ITestResult result) { // TODO Auto-generated method stub } @Override public void onTestSuccess(ITestResult result) { // TODO Auto-generated method stub } @Override public void onTestFailure(ITestResult result) { System.out.println(result.getName() + " : " + result.getThrowable()); } @Override public void onTestSkipped(ITestResult result) { // TODO Auto-generated method stub } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) { // TODO Auto-generated method stub } @Override public void onStart(ITestContext context) { // TODO Auto-generated method stub } @Override public void onFinish(ITestContext context) { // TODO Auto-generated method stub } }
In the above program, result.getName() will give the method name which is failed and result.getThrowable() will give the exact error message.
Below is the testng java configuration file which is the replacement for testng.xml file:
import java.util.ArrayList; import java.util.List; import org.testng.TestNG; import org.testng.xml.XmlClass; import org.testng.xml.XmlSuite; import org.testng.xml.XmlTest; public class TestNgXmlUsingCode { public void testNgXmlSuite() { List<XmlSuite> suites = new ArrayList<XmlSuite>(); List<XmlClass> classes = new ArrayList<XmlClass>(); List<Class> listenerClasses = new ArrayList<Class>(); XmlSuite suite = new XmlSuite(); suite.setName("ProgramSuite"); XmlTest test = new XmlTest(suite); test.setName("ProgramTest"); XmlClass clss1 = new XmlClass("SampleProgramOne"); classes.add(clss1); XmlClass clss2 = new XmlClass("SampleProgramTwo"); classes.add(clss2); listenerClasses.add(ListenerTest.class); test.setXmlClasses(classes); suites.add(suite); TestNG tng = new TestNG(); tng.setXmlSuites(suites); tng.setListenerClasses(listenerClasses); tng.run(); } public static void main(String[] args) { TestNgXmlUsingCode tnxuc = new TestNgXmlUsingCode(); tnxuc.testNgXmlSuite(); } }
The above program is the testng configuration file which is replacement of testng.xml file creating programmatically. By executing the above java program normally we can achieve the same functionality which is available with xml file.
Below is the output of the above program:
This output look is different from the testng,.xml configuration file output.
In the above program, XmlSuite is equal to suite tag in testng.xml file, XmlTest is equal to test tag, XmlClass is equal to class tag.
This way we can create testng.xml file programmatically in TestNG.
Please watch You tube video for better understanding.