TestNG Annotations [ 2024 ]
TestNG Annotations
TestNG is a powerful Java testing framework that provides many features to simplify test creation and management. One key feature is its use of annotations, which help control the execution of tests.
TestNG uses the annotations feature to build an execution framework. This section will discuss some important TestNG annotations frequently used in TestNG tests.
Annotations
Annotations are a Java feature used to add metadata to Java code. They allow information to be added to Java classes, methods, variables, etc., and provide additional information about the code to the Java compiler. We use predefined annotations and also define custom annotations.
For example, sample Java Annotations:
- @Override
- @SupressWarnings
- @Deprecated
TestNG Annotations
- Before and After annotations @BeforeX /@AfterX
- @Test
- @Parameters
- @DataProvider
- @Factory
- @Listeners
@Test is the most basic annotation and is essential for any method you want to run as a test.
@BeforeMethod and @AfterMethod are useful for setting up and cleaning up before and after each test method.
@BeforeClass and @AfterClass are useful for one-time setup and teardown for all tests in a class.
@BeforeSuite and @AfterSuite are useful for setup and teardown that apply to all tests in a suite.
@DataProvider allows the same test to run with multiple data sets, enhancing test coverage.
Code Example
Let’s look at a sample test that uses the @Test, @BeforeTest, and @AfterTest annotations. It is an Appium mobile test.
package com.testingdocs.appium.quickstart.AppiumProject; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import io.appium.java_client.AppiumDriver; import io.appium.java_client.remote.MobileCapabilityType; /** * Sample Appium Test. * www.TestingDocs.com - TestNG Tutorials */ public class SampleTest { public static AppiumDriver<?> mDriver; @BeforeTest public void setup( ) throws MalformedURLException { DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome"); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "10.0"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Androidemulator"); caps.setCapability("avd","Nexus_5X_API_29_x86"); mDriver = new AppiumDriver<> (new URL("http://127.0.0.1:4723/wd/hub"), caps); mDriver.manage().timeouts().implicitlyWait(120,TimeUnit.SECONDS); } @Test public static void titleTest(){ mDriver.get("https://www.testingdocs.com/"); Assert.assertEquals(mDriver.getTitle(), "Home | TestingDocs", "Check Title"); } @AfterTest public void tearDown( ){ mDriver.quit(); } }
The @BeforeTest sets the mobile device and browser for the test and instantiates a mobile test driver. The test method only concentrates on the test logic of the test case. The @AfterTest kills the driver session after the test.
TestNG Annotations Order Example
TestNG Tutorials
TestNG Tutorials on this website can be found at:
For more details on the TestNG Framework, visit the official website of TestNG at: