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.
Author: Amod Mahajan
My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw 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