How will you run a prerequisite method and post condition method for
every test in TestNG?


We can run a prerequisite method and post condition method for every test in TestNG with the help of @BeforeMethod and @AfterMethod annotations.

Example

@BeforeMethod
public void prerequisite(){
   System.out.println("Run before every tests");
}
@AfterMethod
public void postcondition(){
   System.out.println("Run after every tests ");
}
@Test
public void loanPay(){
   System.out.println("Loan pay is successful");
}

In the Java class file, the prerequisite() method with @BeforeMethod will be executed which is known as the precondition for every test method. Then loanPay() will be executed and finally the postcondition() method with @AfterMethod will run.

Updated on: 11-Jun-2020

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements