Testng

From G1Joshi/Agent-Skills · View interactive version

Testng is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert TestNG testing assistance covering annotations, groups, parallel execution, and data providers. Use when writing enterprise Java tests, configuring `testng.xml`, or automating regression suites.

How to use this skill

Skill file

TestNG

TestNG is a testing framework setup inspired by JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as parallel testing and data-driven testing.

When to Use

Quick Start

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGExample {
    @Test(groups = { "fast" })
    public void testAdd() {
        Assert.assertEquals(1 + 1, 2);
    }
}
<!-- testng.xml -->
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" parallel="methods" thread-count="5">
  <test name="test1">
    <classes>
       <class name="com.example.TestNGExample"/>
    </classes>
  </test>
</suite>

Core Concepts

Declarative testng.xml Suite Management

Orchestrates multi-suite, multi-thread test runs across packages:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Enterprise Regression Suite" parallel="tests" thread-count="4">
    <test name="Chrome Tests">
        <parameter name="browser" value="chrome"/>
        <classes>
            <class name="com.example.tests.CheckoutTest"/>
        </classes>
    </test>
</suite>

Method Dependencies (`dependsOnMethods`)

Ensures downstream tests execute only if prerequisite tests pass:

import org.testng.annotations.Test;
import org.testng.Assert;

public class UserWorkflowTest {
    @Test
    public void createUser() {
        Assert.assertTrue(api.register("user@test.com"));
    }

    @Test(dependsOnMethods = {"createUser"})
    public void loginUser() {
        Assert.assertTrue(api.login("user@test.com"));
    }
}

Native DataProviders

Feeds parameterized data to test methods:

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

public class CalculationTest {
    @DataProvider(name = "taxData")
    public Object[][] provideTaxData() {
        return new Object[][] {
            { 100.0, "NY", 108.875 },
            { 100.0, "CA", 107.25 }
        };
    }

    @Test(dataProvider = "taxData")
    public void testTaxCalculation(double amount, String state, double expected) {
        Assert.assertEquals(TaxCalculator.calculate(amount, state), expected, 0.01);
    }
}

Common Patterns

Parallel Test Execution with Groups

Problem: Large regression test suites take hours to run sequentially in CI pipelines.

Solution:

Organize tests into groups and configure parallel execution in testng.xml:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="RegressionSuite" parallel="methods" thread-count="4">
  <test name="SmokeTests">
    <groups>
      <run>
        <include name="smoke"/>
      </run>
    </groups>
    <classes>
      <class name="com.example.OrderTest"/>
      <class name="com.example.UserTest"/>
    </classes>
  </test>
</suite>

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------------------- | :------------------------------------------------------------------------ | :------------------------------------------------------------------------------- |

| Cannot find class in classpath | Class path in testng.xml has typo or build didn't compile test classes. | Verify package name and run mvn test-compile first. |

| TestNGException: Method requires a @DataProvider | DataProvider name does not match declared name or signature is invalid. | Ensure @DataProvider(name = "x") returns Object[][] or Iterator<Object[]>. |

| ConcurrentModificationException in parallel run | Non-thread-safe state shared across test threads. | Use ThreadLocal for drivers/contexts or isolate state per test instance. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.