TestNG Annotations
Before Method vs Before Test
Before Method vs Before Test Code
package asc; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class loginTest { @BeforeTest public void loginToApplication() { System.out.println("Login to Application"); } @AfterTest public void logoutToApplication() { System.out.println("Logout To Application"); } @BeforeMethod public void connectedToDB() { System.out.println("Connected to DB"); } @AfterMethod public void disConnectedFromDB() { System.out.println("Disconnected Form DB"); } @Test(priority=1,description="This is the login Test") public void aTest1() { System.out.println("Test 1"); } @Test(priority=2,description="This is the logout Test") public void aTest2() { System.out.println("Test 2"); } }
This code is a basic example of how to use TestNG annotations in a Java test class. It demonstrates the structure and lifecycle of test methods in a TestNG framework.
Here’s a detailed explanation of each part of the code:
Package Declaration
package asc;
This declares that the class loginTest is part of the package asc.
Class Definition
public class loginTest {
This defines a class named ' loginTest '. This class contains the setup, teardown, and test methods that will be executed by the TestNG framework.
Before and After Annotations
@BeforeTest
@BeforeTest public void loginToApplication() { System.out.println("Login to Application"); }
This method is annotated with " @BeforeTest ", which means it will run only once before any of the test methods in the current test suite are executed. Typically, you use this to perform any setup required for the entire test suite, like logging into an application.
@AfterTest
@AfterTest public void logoutToApplication() { System.out.println("Logout To Application"); }
This method is annotated with " @AfterTest ", meaning it will run only once after all the test methods in the current test suite have run. This is typically used to perform any cleanup required after all the tests, like logging out of an application.
@BeforeMethod
@BeforeMethod public void connectedToDB() { System.out.println("Connected to DB"); }
This method is annotated with " @BeforeMethod ", which means it will run before each test method. This istypically used for setting up the state required for each test, such as connecting to a database.
@AfterMethod
@AfterMethod public void disConnectedFromDB() { System.out.println("Disconnected From DB"); }
This method is annotated with " @AfterMethod ", meaning it will run after each test method. This is used to clean up the state after each test, such as disconnecting from a database.
Test Methods
Test Method 1
@Test(priority=1,description="This is the login Test") public void aTest1() { System.out.println("Test 1"); }
This method is annotated with " @Test ", indicating it is a test method. The priority=1 attribute specifies that this test should run first among other tests. The description attribute provides a brief description of the test. This method simply prints "Test 1" to the console.
Test Method 2
@Test(priority=2,description="This is the logout Test") public void aTest2() { System.out.println("Test 2"); }
Similar to the first test method, this method is also annotated with " @Test ". The priority=2 attribute specifies that this test should run after the test with priority 1. The description attribute provides a brief description of the test. This method simply prints "Test 2" to the console.
Execution Flow
- loginToApplication() (annotated with @BeforeTest) runs once before any test methods.
- connectedToDB() (annotated with @BeforeMethod) runs before each test method.
- aTest1() (annotated with @Test, priority 1) runs.
- disConnectedFromDB() (annotated with @AfterMethod) runs after aTest1().
- connectedToDB() runs again before the next test method.
- aTest2() (annotated with @Test, priority 2) runs.
- disConnectedFromDB() runs after aTest2().
- logoutToApplication() (annotated with @AfterTest) runs once after all test methods.
This structure helps to manage setup and teardown processes efficiently and ensures a clean environment for each test method.