TestNG Inheritance – With Simple Example Program

TestNG Inheritance: We have already learned how testNG supports both method overloading and method overriding in a testNg class. Now, we will discuss one more important oops concept inheritance with TestNG.

If you recall, inheritance is a process by which one class acquires another class’s data member and member function. If you don’t know in detail about inheritance, then there is no need to worry because we have already covered inheritance in Java in detail there.

TestNG Inheritance Implementation

Let’s try to find out if TestNg supports the feature inheritance or not with some simple program:

Superclass:

package com.softwaretestingo.testng;
import org.testng.annotations.Test;
public class SuperTestNgClass 
{
	@Test
	public void superTestNgMethod()
	{
		System.out.println("Super testng method");
	}
}

Child Class with Extends Superclass:

package com.softwaretestingo.testng;
import org.testng.annotations.Test;
public class SubTestNGClass extends SuperTestNgClass
{
	@Test
	public void subTestNgMethod()
	{
		System.out.println("Sub testng method");
	}
}

Now, create a test suite that has both super and child classes and execute that suite.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<test thread-count="5" name="Test">
		<classes>
			<class name="com.softwaretestingo.testng.SuperTestNgClass" />
			<class name="com.softwaretestingo.testng.SubTestNGClass" />
		</classes>
	</test> <!--
	Test -->
</suite> <!--
Suite -->

As in the output, you can see that we have executed both class methods. As a part of the superclass, the method is executed, and as the child class has both super and child class methods, the output has all the methods, which means both super and child class methods.

Output:

Super testng method
Sub testng method
Super testng method

===============================================
Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

3 thoughts on “TestNG Inheritance – With Simple Example Program”

  1. can the order of execution of the tests in child class be controlled , as in when child class runs can we trigger the super class function first and then the child class

    Reply
  2. While executing the same above code ..FAILED CONFIGURATION: @AfterClass test1
    java.lang.NullPointerException: Cannot invoke “org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)” because “ui.test1” is null

    Reply

Leave a Comment