Skip to content Skip to sidebar Skip to footer

ZeroToHero TestNG Framework - From Basics to Advanced

ZeroToHero TestNG Framework - From Basics to Advanced

TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit but designed to simplify testing by making it more flexible and powerful. 

Enroll Now

It supports test configurations, multiple test types, parameterized testing, data-driven testing, and more. If you're a developer or tester, learning TestNG will significantly enhance your testing capabilities, allowing you to go from "Zero to Hero." This article takes you through the basics and builds up to more advanced features.

1. Introduction to TestNG

TestNG was created by CĂ©dric Beust to overcome limitations found in JUnit. Its primary goal is to simplify and optimize testing by providing a feature-rich testing framework. TestNG provides a broad range of functionality, including:

  • Annotations: Simple and intuitive annotations make writing tests easier.
  • Test Configuration: Flexible configuration and parameterization of tests.
  • Parallel Testing: Execute tests concurrently, reducing execution time.
  • Test Suites: Organize multiple tests into groups and suites.

Before diving into more advanced features, it's crucial to understand the basic setup and execution of TestNG.

1.1 Setting Up TestNG

To get started with TestNG in a Maven-based project, add the TestNG dependency to your pom.xml file:

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

If you're not using Maven, you can download the TestNG JAR from the official website and include it in your project manually.

Once you have TestNG set up, you can create your first test class. The most basic TestNG test looks like this:

java
import org.testng.annotations.Test; public class BasicTest { @Test public void testMethod() { System.out.println("This is a basic TestNG test."); } }

1.2 Running Tests

To run TestNG tests, you can do it via an IDE like Eclipse, IntelliJ IDEA, or command line:

  • In Eclipse/IntelliJ: Right-click on the test class and select Run As > TestNG Test.
  • Command line: You can run tests using Maven by typing mvn test.

This will execute the test and print the results to the console. TestNG automatically handles test methods marked with @Test.


2. Basic Features of TestNG

Now that you have your basic test running, let’s explore some fundamental features of TestNG.

2.1 Annotations

TestNG provides a variety of annotations to control the behavior of test methods and lifecycle:

  • @BeforeSuite, @AfterSuite: Runs before or after all tests in the suite.
  • @BeforeTest, @AfterTest: Executes before or after the entire test.
  • @BeforeClass, @AfterClass: Executes once before or after all tests in a class.
  • @BeforeMethod, @AfterMethod: Executes before or after each test method.

Here’s an example demonstrating some of these annotations:

java
import org.testng.annotations.*; public class AnnotationExample { @BeforeClass public void setUpClass() { System.out.println("Before the class"); } @AfterClass public void tearDownClass() { System.out.println("After the class"); } @BeforeMethod public void setUp() { System.out.println("Before each test method"); } @AfterMethod public void tearDown() { System.out.println("After each test method"); } @Test public void testOne() { System.out.println("Test One"); } @Test public void testTwo() { System.out.println("Test Two"); } }

2.2 Grouping Tests

TestNG allows you to group tests for better organization. You can assign test methods to groups and run them selectively.

java
@Test(groups = {"smoke"}) public void testMethodOne() { System.out.println("Smoke test method"); } @Test(groups = {"regression"}) public void testMethodTwo() { System.out.println("Regression test method"); }

You can run specific groups by configuring them in your testng.xml file:

xml
<suite name="MyTestSuite"> <test name="MyTests"> <groups> <run> <include name="smoke"/> </run> </groups> <classes> <class name="com.example.AnnotationExample"/> </classes> </test> </suite>

2.3 Assertions

TestNG provides assertions to validate the expected outcome of your tests. The Assert class allows you to check conditions such as equality, nullity, and true/false states.

java
import org.testng.Assert; import org.testng.annotations.Test; public class AssertionExample { @Test public void testEquality() { int actual = 10; int expected = 10; Assert.assertEquals(actual, expected); } @Test public void testNotNull() { String actual = "TestNG"; Assert.assertNotNull(actual); } }

3. Intermediate TestNG Features

Now that you’re comfortable with basic TestNG concepts, let’s move on to some intermediate features that make TestNG stand out.

3.1 Parameterized Tests

TestNG allows parameterizing your tests from the XML file or using the @Parameters annotation.

java
import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParameterizedTest { @Test @Parameters({"browser", "version"}) public void testWithParameters(String browser, String version) { System.out.println("Browser: " + browser); System.out.println("Version: " + version); } }

You configure the parameters in the testng.xml file:

xml
<suite name="Suite"> <test name="Test with Parameters"> <parameter name="browser" value="Chrome"/> <parameter name="version" value="91.0"/> <classes> <class name="com.example.ParameterizedTest"/> </classes> </test> </suite>

3.2 DataProvider for Data-Driven Testing

TestNG’s @DataProvider annotation allows running the same test with different sets of data. This is particularly useful for testing multiple input variations.

java
import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DataDrivenTest { @DataProvider(name = "testData") public Object[][] createData() { return new Object[][]{ {"John", 30}, {"Jane", 25}, {"Doe", 40} }; } @Test(dataProvider = "testData") public void testDataProvider(String name, int age) { System.out.println("Name: " + name + ", Age: " + age); } }

This way, TestNG will run the test method for each set of data.

3.3 Parallel Execution

TestNG supports parallel execution of tests, helping reduce the time it takes to run your test suite. You can configure this in the testng.xml file by adding the parallel attribute.

xml
<suite name="ParallelSuite" parallel="methods" thread-count="4"> <test name="ParallelTest"> <classes> <class name="com.example.ParallelTest"/> </classes> </test> </suite>

In this example, test methods will run in parallel across four threads.


4. Advanced TestNG Features

Once you've grasped intermediate concepts, you're ready to leverage advanced TestNG features to optimize your test automation.

4.1 Dependency Management

You can control the execution order of tests using the dependsOnMethods attribute. This ensures a specific test only runs if its dependencies pass.

java
@Test public void testLogin() { System.out.println("Login test"); } @Test(dependsOnMethods = {"testLogin"}) public void testSearch() { System.out.println("Search test"); }

4.2 Soft Assertions

TestNG provides SoftAssert, which allows multiple assertions within a single test. If one assertion fails, the others continue to execute, and the test reports all assertion failures at the end.

java
import org.testng.asserts.SoftAssert; import org.testng.annotations.Test; public class SoftAssertExample { @Test public void testSoftAssertion() { SoftAssert softAssert = new SoftAssert(); softAssert.assertEquals(10, 20, "First Assertion Failed"); softAssert.assertTrue(false, "Second Assertion Failed"); softAssert.assertAll(); // Collect all assertion errors } }

4.3 Custom Listeners

Listeners in TestNG allow you to create custom behavior when tests start, pass, fail, or skip. You can implement ITestListener and override methods like onTestFailure to capture failure details.

java
import org.testng.ITestListener; import org.testng.ITestResult; public class CustomListener implements ITestListener { @Override public void onTestFailure(ITestResult result) { System.out.println("Test failed: " + result.getName()); } // Implement other methods if needed }

To use the listener, add it to the testng.xml file or annotate your test class:

java
@Listeners(CustomListener.class) public class TestWithListener { @Test public void testFailure() { assert false : "This test should fail"; } }

5. Conclusion

TestNG is a robust framework that supports a wide variety of testing strategies and techniques. Starting from basic test creation, annotations, and assertions, you can quickly move to intermediate concepts like parameterized tests, data-driven testing, and parallel execution. For advanced users, features like dependency management, soft assertions, and custom listeners allow fine control and optimization of your tests.

By mastering TestNG, you can create flexible, maintainable, and efficient automated test suites, ultimately enabling you to go from Zero to Hero in software testing.

LLM Engineering: Master AI & Large Language Models (LLMs) Udemy

Post a Comment for "ZeroToHero TestNG Framework - From Basics to Advanced"