Javatpoint Logo
Javatpoint Logo

TestNG @BeforeMethod Annotation

The @BeforeMethod is specific to a class not to an XML file. The @BeforeMethod annotated method will be invoked before the execution of each test method where the test method is nothing but a test case. Suppose there are four test methods in a class then the @BeforeMethod annotated method is executed before the execution of each test method. If there are four test methods, then four times @BeforeMethod annotated method will be invoked.

Let's understand the @BeforeMethod annotation through an example.

Step 1: Open the Eclipse.

Step 2: We create a simple java project which contains @BeforeMethod annotation.

Before_Methods.java

In the above code, we created the @BeforeMethod annotated method which will be invoked before the execution of each test method, i.e., sum() and difference() test methods.

Step 3: Now, we create the testng.xml file to configure the Before_Methods class.

testng.xml file

Step 4: Run the testng.xml file. Right click on the testng.xml file and move the cursor down to Run As and then click on the 1 TestNG Suite.

Output

TestNG @BeforeMethod Annotation

In the above output, difference() method executes before the sum() as we know that TestNG runs the test methods in alphabetical order, the @BeforeMethod annotated method is invoked before the execution of each test method, i.e., the difference() and sum().


Next TopicTestNG Annotations




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA