0

When using TestNG, If multiple test classes inherit a class containing a @BeforeTest method, will it run only once or everytime when each of the classes are called in the test case?

When I run the below code, the text "BEFORE TEST EXECUTED" is printed only once. Though both the test classes have this method inherited.

public class masterclass
{

@BeforeTest
public void show()
 {
 sout("BEFORE TEST EXECUTED")
 }
}

Test Class-1:

public class TestClass1 extends masterclass
 {
    @Test
    public void testA1() { }
}

Test Class-2:

public class TestClass2 extends masterclass
{
    @Test
    public void testB1() {}    
}

XML of the test Case :

<suite  name="testSuite">
<test name="MytestCase" >
    <classes>
        <class name="com.ui.test.TestClass1" />
        <class name="com.ui.test.TestClass2" />
    </classes>
</test>
</suite>
Muzzamil
  • 2,823
  • 2
  • 11
  • 23
anandhu
  • 686
  • 2
  • 13
  • 40
  • I can't understand question. Can you please provide sample code? Please check it too https://meta.stackexchange.com/questions/120219/how-do-you-ask-a-question-on-stack-overflow – Muzzamil Dec 17 '19 at 08:48
  • I have added sample code. Since the BeforeTest method is inherited by both classes in the test case, and TestNG will execute all BeforeTests in all the classes inside a test case at the beginning of execution of that test case, it should execute the BeforeTest two times right? – anandhu Dec 17 '19 at 09:02
  • What is exactly your issue? You are asking your doubt or it is a problem/issue ? Do you want to call @BeforeTest method before every test case? I just give the logical reason why Before test call will be one time. if you want solution let me know? – Muzzamil Dec 17 '19 at 11:35
  • I am wondering why my @BeforeTest is executed only once in this case. Because since i am having two classes in the test case. And both these classes have this BeforeTest method (Beacuse of inheritance), there should be 2 BeforeTest Executions in total in the beginning of the test case right? But why is this executed only once in this case? – anandhu Dec 17 '19 at 11:48
  • That I have mentioned in answer why it is one time. It is because of annotation @BeforeTest which will execute one time before all test class/Test case in tag in testng.xml. Hope you understand it. – Muzzamil Dec 17 '19 at 11:51
  • So you are saying only one BeforeTest method execution will happen for each test case? If there are many beforetest methods all the rest will be ignored?! – anandhu Dec 17 '19 at 11:58
  • when I say "one BeforeTest method execution will happen for each test case"? I mentioned "annotation BeforeTest which will execute one time before all test class/Test case in tag in testng.xml". Please check it https://stackoverflow.com/questions/15218863/what-is-the-difference-between-test-method-and-test-tag-intestng, I think you are confused between @Test and tag – Muzzamil Dec 17 '19 at 12:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204408/discussion-between-automaticsoldier-and-muzzamil). – anandhu Dec 17 '19 at 12:08

1 Answers1

3

In your case, @BeforeTest will run one time. It will see to Testng.xml and will run before all test cases under <test> tag.

Master Test Class:

public class MasterClass {
    @BeforeTest
    public void show()
     {
     System.out.println("BEFORE TEST EXECUTED");
     }

}

Test Class-1:

public class TestClass1 extends MasterClass{

      @Test
       public void test()
          {
          System.out.println("Priority 0 (Default in Class 1)");
          }
   }

Test Class-2:

public class TestClass2 extends MasterClass{


      @Test(priority = 0)
       public void testin_LearningTestNGTest2_Class()
          {
          System.out.println("Priority 0 (Class 2)");
          }
   }

TestNG.xml:

<suite  name="testSuite">
<test name="MytestCase">
    <classes>
        <class name="com.ui.test.TestClass1" />
        <class name="com.ui.test.TestClass2" />
    </classes>
</test>
</suite>

Output:

BEFORE TEST EXECUTED
Priority 0 (Default in Class 1)
Priority 0 (Class 2)

Hope it will help you.

Muzzamil
  • 2,823
  • 2
  • 11
  • 23
  • Please accept it as answer if it solved your problem. It will be helpful for people have same issue in future. Thanks – Muzzamil Dec 17 '19 at 10:25
  • I am wondering why my @BeforeTest is executed only once in this case. Because since i am having two classes in the test case. And both these classes have this BeforeTest method (Beacuse of inheritance), there should be 2 BeforeTest Executions in total in the beginning of the test case right? But why is this executed only once in this case? – anandhu Dec 17 '19 at 11:49
  • if you will change BeforeTest to @BeforeMethod in MasterClass then it will serve your purpose. – Muzzamil Dec 17 '19 at 11:53