Both annotations runs before the @test in testNG then what is the difference between two of them.
@BeforeTest : It will call Only once, before Test method.
@BeforeMethod It will call Every time before Test Method.
Example:
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Test_BeforeTestAndBeforeMethod {
@BeforeTest
public void beforeTestDemo()
{
System.out.println("This is before test calling.");
}
@BeforeClass
public void beforeClassDemo()
{
System.out.println("This is before class calling.");
}
@BeforeMethod
public void beforeMethodDemo()
{
System.out.println("This is before method calling.");
}
@Test
public void testADemo()
{
System.out.println("This is Test1 calling.");
}
@Test
public void testBDemo()
{
System.out.println("This is Test2 calling.");
}
@Test
public void testCDemo()
{
System.out.println("This is Test3 calling.");
}
@AfterMethod
public void afterMethodDemo()
{
System.out.println("This is after method calling.");
}
@AfterClass
public void afterClassDemo()
{
System.out.println("This is after class calling.");
}
@AfterTest
public void afterTestDemo()
{
System.out.println("This is after test calling.");
}
}
@BeforeTest
will execute only one time before any test methods. Methods will run before executing any @Test
annotated test method that is part of the <test>
tag in testNG.xml file.
@BeforeMethod
will execute before every method annotated with @Test
.
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
@BeforeTest To execute a set-up method before any of the test methods included in the < test > tag in the testng.xml file. @BeforeMethod To execute a set-up method before any of the test methods annotated as @Test.
@BeforeTest
- runs before each test declared inside testng.xml
@BeforeMethod
- runs before each test method declared within class and under an @Test
annotation
@BeforeTest
: It will be called Only one time befor any test methods, no matter how many method annotaed with @Test
, it will be called only one time
@BeforeMethod
It will be called befor every methode annotated with @Test
, if you have 10 @Test
methods it will be called 10 times
To know what is the Difference between BeforeClass
and BeforeTest
, please refer to the answer https://stackoverflow.com/a/57052272/1973933