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

 

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 to Souni Cancel reply

Your email address will not be published. Required fields are marked *