DEV Community

Cover image for TestNG Annotations and It's uses
rohits613
rohits613

Posted on

TestNG Annotations and It's uses

In software testing, accuracy, organization, and efficiency are essential. TestNG annotations, which have become a shining example of these attributes, have altered the testing environment for Java programmes. Testing has changed from a routine chore to a deliberate process that orchestrates test execution with expertise and precision thanks to annotations.

What is annotation?
Programming annotations are labels or markers that are included into the source code as metadata to give the compiler, runtime environment, or other tools more information. Annotations provide different tools and frameworks with instructions, configuration options, or context rather without altering the underlying logic of the code.

A special syntax that uses symbols like @ is frequently followed by the name of the annotation and any associated parameters. Annotations can be used, among other things, to specify configuration information, document code, activate or disable particular behavior, or mark code for further processing.

What is TestNG Framework ?
A Java open-source project named TestNG, or Test Next Generation, was created to simplify automated testing. It keeps the core JUnit foundation but adds a tonne of brand-new features and capabilities. Developers and testers can easily organize, schedule, and execute tests thanks to TestNG’s features. Test grouping, simultaneous test execution, parameterized testing, test dependencies, and thorough reporting are just a few of the essential characteristics. Test settings and processes are specified using annotations. In the Java and Java-related development communities, TestNG is extensively used for unit testing, integration testing, and end-to-end testing to assess the dependability and quality of software systems.

Different Annotation in TestNG
TestNG Annotations are lines of code added to the programme to specify how the procedure underneath them should be executed.Below is the list of TestNG annotations along with its explanation and example;

@test: This is one of the core annotations in TestNG. This annotation designates a method as a test case. TestNG is told to run the methods as independent test cases via this annotation.You can specify additional characteristics, highlight dependencies, and enable or deactivate tests using this annotation.

import org.testng.annotations.Test;

public class First_Test {

      @Test
      public void Test_Method() 
      {
 // Test logic goes here
       }
 }
Enter fullscreen mode Exit fullscreen mode

@test(enabled = false): You can momentarily disable a test method without deleting it from your codebase by annotating @test(enabled = false).

@BeforeMethod and @AfterMethod: Each test method in the current class should have a method called @BeforeMethod run both before and after it. useful for typical pre-test preparation and clean-up following tests.

`import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;

public class MyTest {

@BeforeMethod
public void setUp() {
    // Perform setup actions before each test method
}

@Test
public void test1() {
    // Test logic for test1
}

@Test
public void test2() {
    // Test logic for test2
}

@AfterMethod
public void tearDown() {
    // Perform teardown actions after each test method
}
Enter fullscreen mode Exit fullscreen mode

}`

@BeforeClass and @AfterClass: A method designated as @BeforeClass and @AfterClass shall execute once before and once after all test methods in the current class, respectively. useful for setting up and taking down classes.

import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class MyTest {

    @BeforeClass
    public void classSetUp() {
        // Perform setup actions before all test methods in this class
    }

    @Test
    public void test1() {
        // Test logic for test1
    }

    @Test
    public void test2() {
        // Test logic for test2
    }

    @AfterClass
    public void classTearDown() {
        // Perform teardown actions after all test methods in this class
    }
}
Enter fullscreen mode Exit fullscreen mode

@DataProvider: The method that provides data for parameterized testing is specified by this annotation. By providing several types of data, it enables you to design parameterized tests.To get test data for your parameterized test methods, TestNG will call this function.For test data, the data provider method returns either a two-dimensional array or an IteratorObject[]>.

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class MyTest {
@DataProvider(name = "testData")
public Object[][] testData() {
    return new Object[][] {
            {"Data1"},
            {"Data2"},
            {"Data3"}
        };
    }

    @Test(dataProvider = "testData")
    public void testWithData(String data) {
        // Test logic using data
    }
}
Enter fullscreen mode Exit fullscreen mode

@BeforeSuite and @AfterSuite: These annotations, @BeforeSuite and @AfterSuite, in a test configuration file specify which operations are performed before and after each test suite. For international setup and disassembly, frequently used.

import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

public class MyTest {

    @BeforeSuite
    public void suiteSetUp() {
        // Perform setup actions before all test suites
    }

    @Test
    public void test1() {
        // Test logic for test1
    }

    @AfterSuite
    public void suiteTearDown() {
        // Perform teardown actions after all test suites
    }
}

Enter fullscreen mode Exit fullscreen mode

@BeforeTest and @AfterTest: Similar to suite-level annotations, @BeforeTest and @AfterTest in your test configuration XML indicate the test methods that are run before and after all other test methods within a certain element.

import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class ExampleTest {

    @BeforeTest
    public void beforeTest() {
        // Set up resources before the test suite
        System.out.println("BeforeTest: Setting up resources...");
    }

    @AfterTest
    public void afterTest() {
        // Clean up resources after the test suite
        System.out.println("AfterTest: Cleaning up resources...");
    }

    // Test methods go here
}
Enter fullscreen mode Exit fullscreen mode

@Parameters: Indicates a method parameter’s value should be retrieved from the test suite’s XML file based on the parameter name.It is typically used in conjunction with @DataProvider to pass parameters to test methods.

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class MyTest {
    @Test
    @Parameters("browser")
    public void testWithParameter(String browser) {
        // Test logic based on the 'browser' parameter
    }
}
Enter fullscreen mode Exit fullscreen mode

@Listeners: This annotation specifies one or more listener classes for your test suite that should be alerted of test events. Listeners have the option to intervene before and after specific events, such as the start of a test, its failure, and the end of the suite.You can create your own listeners to respond in a certain way to these occurrences.


import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(MyListener.class)
public class ListenerExampleTest {

    @Test
    public void testMethod() {
        System.out.println("Executing testMethod");
    }
}

Enter fullscreen mode Exit fullscreen mode

Testng Annotations Order of Execution

TestNG provides a hierarchy of annotations that helps to organize and execute test methods in a logical order. The hierarchy of TestNG annotations is as follows:

@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite

Benefit of using TestNG Annotation

For effective and well-structured test automation, TestNG annotations are required. They structure and delineate test code, which makes it simple to recognise and comprehend test setups, methods, and teardowns. Prioritisation, parallelization, and parameterization are made possible through annotations, which provide granular control over test execution. This change improves test suite functionality and makes test administration easier. Annotated code furthermore acts as the codebase’s “living documentation,” revealing test objectives and preserving test relevance and dependability while the software is improved. Essentially, TestNG annotations improve the organization, personalization, and documentation of tests, resulting in more dependable and efficient automated testing procedures.

Pre-requirement for TestNG

The prerequisites for utilizing TestNG in your Java projects are, in a word, as follows:

  • Java Development Kit (JDK): Ensure that your computer has the Java Development Kit (JDK) installed.

  • Integrated Development Environment (IDE): Use an IDE like Eclipse or IntelliJ for easier test development.

  • Installation of TestNG: Using your project’s build tool (Maven, Gradle, or IDE), add TestNG as a dependency.You can also download the TestNG JAR.

  • Project Setup: Create or launch the Java project where you’ll be developing and running TestNG tests.

  • Test Classes and Methods: Create Java test classes that contain @Test-annotated methods.

  • TestNG XML Configuration: Create a TestNG XML configuration file if necessary for advanced test settings.

  • Annotations: To control a test’s flow, recognise and use TestNG annotations.

  • Test Data and Dependencies: Make a plan to account for test dependencies and data (optional).

  • Logging and Reporting: A choice should be made about your logging and reporting methods.

  • Continuous Integration (CI) Integration: If necessary, prepare to include TestNG tests in your CI/CD workflow. Integration using continuous integration (CI).

Working of TestNG Annotation

The TestNG workflow can be summed up by a few key stages:
Write Tests: Use the @test annotation provided by TestNG to write tests for Java test classes with annotated methods.
Configuration:When configuring, it is ideal to specify test groups, dependencies, and other settings in a TestNG XML file.
Organize: Group pertinent tests using XML settings or annotations.
Run tests: To run tests (using the command line, an IDE, or build tools), use the TestNG framework.
Reports: TestNG generates detailed, examinable test reports.
Analyze: Examine reports to spot issues and address them.
Iteration: By changing the code and rerunning the tests, you can keep your test suite up to date while iterating.
CI/CD:So that you may execute automated testing as you create, include tests in your CI/CD workflow.
Maintain: Continue to run your tests as your programme gets better.
By automating the testing procedure and providing information on the state of the code, this workflow helps with the assurance of software quality.

Here is a simplistic Java example of utilizing TestNG for a fundamental test scenario.

package base;


import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;


public class Configuration_Annotations {


@BeforeSulte
public void beforeSuite()
{
    System.out.println("Its a beforesuite annotation");
}


@BeforeTest
public void beforeTest()
{
    System.out.println("Its a beforetest annotation");
}


@BeforeClass
public void beforeClass()
{
    System.out.println("Its a beforeclass annotation");
}


@BeforeMethod
public void beforeMethod()
{
    System.out.println("Its a beforemethod annotation");
}


@Test
public void scheduleAppointment()
{
    System.out.println("Its a test annotation");
}


@AfterMethod
public void afterMethod()
{
     System.out.println("Its an aftermethod annotation");
}


@AfterClass
public void afterClass()
{
    System.out.println("Its an afterclass annotation");
}


@AfterTest
public void afterTest()
{
    System.out.println("Its an aftertest annotation");
}


@AfterSuite
public void afterSuite()
{
    System.out.println("Its an aftersuite annotation");
}
}
Enter fullscreen mode Exit fullscreen mode

In the above example; the annotations will be executed in the following order:

@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@test
@AfterMethod

Advantages of TestNG over Junit

TestNG has a lot of benefits over more conventional testing frameworks like JUnit for automated testing. Some of the key advantages of TestNG include the following:

Listeners: You can configure listeners to carry out tasks before and after tests, suites, and other events with the aid of TestNG’s listener interface. This functionality enables the addition of customized setup, takedown, reporting, and cleanup procedures.
Reporting: TestNG instantly creates detailed test execution reports. These reports offer statistics, failure rate data, and test results. To enhance and customize reporting features, you can also make use of alternative reporting tools.
Parameterized testing: With the help of TestNG’s parameterized testing capabilities, you may execute the same test procedure on several sets of input data. Because of this, many different possible outcomes may be assessed with minimum code repetition.
Timeout: Making sure that tests don’t run indefinitely is accomplished by setting timeouts for test methods. Finding and handling test failures brought on by protracted processes is much easier with this.
Parallel Execution: One of TestNG’s distinctive features is its ability to run tests concurrently across a variety of threads and even test suites. Because it can significantly lower the overall test running time, this is excellent for large test suites.
Cross-browser testing: As part of cross-browser testing, TestNG may be used in conjunction with tools like Selenium WebDriver to test online applications across numerous platforms and browsers.
Annotation: When creating and modifying test methods, test classes, and test suites, TestNG heavily relies on annotations. The order in which tests are run as well as exceptions, timeouts, and other variables can be predicted using annotations.
Test configuration: Test configuration is possible using XML configuration files or programmatically writing code. This can be used to highlight test groups, test method dependencies, and other elements.
Test Hierarchies: Setting up a hierarchical test structure using TestNG involves organizing your tests into suites, groups, and dependents. This is quite helpful for setting up challenging test scenarios.For running unit tests, integration tests, and end-to-end tests on your application, this is incredibly helpful.

Disadvantages of TestNG

  • Setting up TestNG takes time.

  • If your project does not require the prioritizing of test cases, TestNG is worthless.

  • Compared to JUnit, it was formerly used less frequently, hence fewer people had experience with it.

Conclusion

TestNG annotations are an essential component of the TestNG testing framework since they offer a dependable and adaptable method for organizing and maintaining your automated test cases. Developers and testers may design complete, effective, and maintainable test suites by combining and categorizing tests, parameterizing data, managing dependencies, and enabling parallel execution with the aid of TestNG annotations. Using TestNG annotations could significantly improve the dependability and quality of your final product whether you are running unit tests, integration tests, or end-to-end tests on your Java applications.

Top comments (0)