Make Selenium Easy

TestNG Tutorials 19: Default Priority Of @Test Methods In Inherited TestNG Classes

Hello Folks,

In last post, we have seen default order of test methods in a TestNG class. You go to that post here.

We know that how inheritance works in TestNG class from this post.

In this post, we will see how default priority of test methods works in inherited class.

When a TestNG class inherits another class, @Test methods of superclass also become part of subclass. But when you run it, superclass methods and su class methods will be prioritized and executed separately . Test methods from both the classes will not be sorted and prioritised combined. It will be individual process for both superclass and subclass.

See an example below:

Super class:

Sub class:

Output:

Observe output. You can see TestNG executed methods of superclass and subclass separately. First subclass test methods got executed followed by superclass methods.

 

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

 

Table of Contents

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 19: Default Priority Of @Test Methods In Inherited TestNG Classes

  1. In my previous comment I observe that Parent Class Method ran first and then Child Class methods were run. Both were prioritized and executed seperately. It was individual process for both superclass and subclass.

    Now I tried to provide priority to a method of a Child Class and observed the result.
    I see that both Parent and Child Class are prioritized and executed combined now. Could you guide me what should I conclude?

    Parent Class

    package mainPack;

    import org.testng.annotations.Test;

    public class Parent {

    @Test
    public void z(){
    System.out.println(“I am Parent small z method”);
    }

    @Test
    public void Z(){
    System.out.println(“I am Parent Capital Z method”);
    }

    }

    Child Class

    package mainPack;

    import org.testng.annotations.Test;

    public class Child extends Parent {

    @Test(priority = -1)
    public void negative() {

    System.out.println(“I am negative method”);

    }

    @Test
    public void ab() {

    System.out.println(“ab”);
    }

    @Test
    public void AB() {
    System.out.println(“AB”);
    }

    @Test
    public void $abc() {
    System.out.println(“$abc”);
    }

    @Test
    public void a_b() {

    System.out.println(“a_b”);
    }

    }

    Output:

    [RemoteTestNG] detected TestNG version 7.0.0

    I am negative method
    $abc
    AB
    I am Parent Capital Z method
    a_b
    ab
    I am Parent small z method

  2. Hello Amod,

    I tried doing the same as you done above. However my observation has different result from yours.

    I see that first Parent Class method are run first and then Child Class method are run.

    package mainPack;

    import org.testng.annotations.Test;

    public class Parent {

    @Test
    public void z(){
    System.out.println(“I am Parent small z method”);
    }

    @Test
    public void Z(){
    System.out.println(“I am Parent Capital Z method”);
    }

    }

    package mainPack;

    import org.testng.annotations.Test;

    public class Child extends Parent {

    @Test
    public void ab() {

    System.out.println(“ab”);
    }

    @Test
    public void AB() {
    System.out.println(“AB”);
    }

    @Test
    public void $abc() {
    System.out.println(“$abc”);
    }

    @Test
    public void a_b() {

    System.out.println(“a_b”);
    }

    }

    Output

    [RemoteTestNG] detected TestNG version 7.0.0
    I am Parent Capital Z method
    I am Parent small z method
    $abc
    AB
    a_b
    ab

  3. hi, in previous tutorial of inheritance with testng, super class methods got executed first and then sub class. In this the order is reversed. Also in previous tutorial in super class methods got executed twice. But here it executed only once. Can please explain why this happened.

    1. Check testng.xml in previous example, I have included super class also in xml explicitly. Thats why it ran twice. One for super class and second time because of sub class.

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 19: Default Priority Of @Test Methods In Inherited TestNG Classes

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