Skip to content
Advertisement

How TestNG annotation from base class is executed when the Test inside the derived class is executed?

While learning TestNG on Udemy, I come across a code that I am unable to understand. The instructor has created a class named “TestBase” where he defined @BeforeMethod/@aftermethod.Later he created another class named “LoginTest” where he wrote the actual test with @test. He extended TestBase class in loginTest to get variable initiated in TestBase class. When he ran loginTest then @BeforeMethod/@aftermethod also ran with this. How did these two methods ran along with @test when these methods are in different classes. here are both codes:

JavaScript

Advertisement

Answer

If you read this line :

JavaScript

this clearly tells that, LoginTest is a child class of TestBase .

so TestBase gets more precedence.

Now let’s understand what is @BeforeMethod.

@BeforeMethod

JavaScript

so this is by default Testng architecture to run @BeforeMethod before each @Test in your test suite.

Your program execution should be in this order :-

JavaScript

then

JavaScript

then

JavaScript

if you have more than one @Test, the order should be same.

You can refer here and the above reference has been taken from TestNG official docs.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement