You are currently viewing Tutorial on TestNG with Examples

Tutorial on TestNG with Examples

  • Post author:
  • Post category:TestNG
  • Post comments:0 Comments
  • Post last modified:May 2, 2024

1. Installation

TestNG can be easily added to your project using Maven or Gradle. For Maven, add the following dependency to your pom.xml:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.4.0</version>
    <scope>test</scope>
</dependency>

For Gradle, add this to your build.gradle:

dependencies {
    testImplementation 'org.testng:testng:7.4.0'
}

2. Creating TestNG Test Cases

Let’s create a simple test case to demonstrate TestNG’s features.

import org.testng.annotations.Test;

public class TestNGExample {

    @Test
    public void testAddition() {
        int result = Calculator.add(3, 5);
        Assert.assertEquals(result, 8, "Addition is incorrect");
    }
}

3. Annotations

1. @Test

The @Test annotation is used to mark a method as a test method.

@Test
public void testMethod() {
    // Test code goes here
}

2. @BeforeMethod and @AfterMethod

These annotations are used to perform setup and teardown operations before and after each test method.

@BeforeMethod
public void setup() {
    // Setup operations
}

@AfterMethod
public void teardown() {
    // Teardown operations
}

3. @BeforeClass and @AfterClass

These annotations are used to perform setup and teardown operations before and after the entire test class.

@BeforeClass
public void beforeClass() {
    // Setup operations before the class
}

@AfterClass
public void afterClass() {
    // Teardown operations after the class
}

4. @BeforeSuite and @AfterSuite

These annotations are used to perform setup and teardown operations before and after the entire suite of tests.

@BeforeSuite
public void beforeSuite() {
    // Setup operations before the suite
}

@AfterSuite
public void afterSuite() {
    // Teardown operations after the suite
}

4. Assertions

TestNG provides a set of assertion methods to validate expected results.

import org.testng.Assert;

Assert.assertEquals(actual, expected);
Assert.assertTrue(condition);
Assert.assertFalse(condition);
Assert.assertNotNull(object);
Assert.assertNull(object);

5. TestNG XML Configuration

TestNG allows you to configure tests using an XML file.

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite">
    <test name="MyTest">
        <classes>
            <class name="com.example.TestNGExample"/>
        </classes>
    </test>
</suite>

6. Parameterized Tests

TestNG supports parameterized tests using the @Parameters annotation.

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

public class ParameterizedTest {

    @Test
    @Parameters({"param1", "param2"})
    public void testMethod(String param1, int param2) {
        // Test code with parameters
    }
}

7. Groups

You can group test methods and include or exclude them during test runs.

@Test(groups = {"group1"})
public void testMethod1() {
    // Test code
}

@Test(groups = {"group2"})
public void testMethod2() {
    // Test code
}

8. Dependencies

Define dependencies between test methods using the dependsOnMethods attribute.

@Test
public void loginTest() {
    // Login test
}

@Test(dependsOnMethods = {"loginTest"})
public void dashboardTest() {
    // Dashboard test
}

9. Listeners

Listeners in TestNG allow you to customize the test execution process.

import org.testng.ITestListener;
import org.testng.ITestResult;

public class CustomListener implements ITestListener {

    public void onTestStart(ITestResult result) {
        System.out.println("Test started: " + result.getName());
    }

    public void onTestSuccess(ITestResult result) {
        System.out.println("Test passed: " + result.getName());
    }

    public void onTestFailure(ITestResult result) {
        System.out.println("Test failed: " + result.getName());
    }

    // Other listener methods
}

To use this listener, add it to your test class:

@Listeners(CustomListener.class)
public class MyTestClass {
    // Test methods
}

This concludes our basic tutorial on TestNG with examples. TestNG offers a wide range of features to make testing easier and more organized, making it a popular choice among developers for writing test cases in Java applications.

Leave a Reply