Make Selenium Easy

TestNG Tutorials 15: Inheritance In TestNG Classes

Hello Folks,

In this post, we will learn Inheritance in TestNG classes.

In very basic words, Inheritance is an OOPS concept which allows a class to inherit property of another class for the sake of re-usability. Like any other class we can also apply concept of inheritance in TestNG class. We will do an example first and observe behavior.

Super TestNG class:

Sub class extending above super TestNG class:

Now create a TestNG class from package level:

You can see TestNG includes both super class and sub class as both have @Test annotated methods in it.

Let’s run testng.xml:

You can see both TestNG classes ran as expected following inheritance concept.

Super TestNG class “SuperTestNgClass” has one @Test annotated method which got executed and printed “Super testng method”. Class “SubTestNGClass” extends class “SuperTestNgClass”. So sub class “SubTestNGClass” will have total two @Test annotated methods: One from super class and another its own. So, it executed both methods and printed output. Here TestNG will not wait for any explicit call to super class testng method.

Note: It is an interview question also.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.


Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

5 thoughts on “TestNG Tutorials 15: Inheritance In TestNG Classes

  1. How can I maintain the same inheritance structure but avoid having the same test method from the super class executed twice?

  2. Hi,

    I have one doubt.
    In your testng xml file you have included both the classes from class level. So it is obvious both @test method will run.

    I am confused where inheritance concepts applies here.


    1. Oh sorry initially whole web pages were not leaded successfully. I saw the out put and its clear now. Thanks!

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

TestNG Tutorials 15: Inheritance In TestNG Classes

by Amod Mahajan time to read: 1 min
%d bloggers like this: