Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 16, 2016

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:

1

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.



Share this post: on Twitter on Facebook

Executing Only Failed Tests in TestNG Read Data From Excel Using Column Number

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

PARALLEL EXECUTION OF classes

TestNG

Parallel Execution of Classes In TestNG

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025