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.
#ThanksForReading
#HappySelenium

 

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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.

    Thanks,

    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.
%d bloggers like this: