Both annotations runs before the @test in testNG then what is the difference between two of them.
@BeforeMethod annotated method will be run before each test method i.e say there are three test methods (i.e test cases), then @BeforeMethod annotated method will be called thrice before each test method. The following is a list of attributes supported by the @BeforeMethod annotation: Attribute.
@BeforeTest is executed once before the execution of all methods in all classes within test tag. on the other hand; @BeforeClass is executed once before the execution of all methods within the class it is defined.
Key points in these differences are:@BeforeTest method executes only once before the first @Test method. @BeforeClass executes before each class.
@BeforeTest: The method which comes under the @BeforeTest annotation will be executed first before any test belonging to that folder. ADVERTISEMENT.
check below code and output
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class Test_BeforeTestAndBeforeMethod { @BeforeTest public void beforeTest() { System.out.println("beforeTest"); } @BeforeMethod public void beforeMethod() { System.out.println("\nbeforeMethod"); } @Test public void firstTest() { System.out.println("firstTest"); } @Test public void secondTest() { System.out.println("secondTest"); } @Test public void thirdTest() { System.out.println("thirdTest"); } }
output:
beforeTest beforeMethod firstTest beforeMethod secondTest beforeMethod thirdTest
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With