Showing posts with label Unit Testing in Java. Show all posts
Showing posts with label Unit Testing in Java. Show all posts

TestNG groups

  



What are Testng Groups?

In Testnggroups are used to categorize your test methods logically so you can run:

  • A specific set of tests (like "smoke", "regression", "sanity")

  • Multiple tests together, even if they are in different classes

Groups allow modular test execution, making large test suites more manageable and efficient.

TestNG Groups revolves around the concept of categorization and selective execution of test methods. They provide a flexible mechanism to label, filter, and run subsets of your total test suite based on defined criteria, without altering the structure of your test classes.The theory of TestNG Groups revolves around the concept of categorization and selective execution of test methods. They provide a flexible mechanism to label, filter, and run subsets of your total test suite based on defined criteria, without altering the structure of your test classes.


1. Categorization and Tagging Theory

At its core, a TestNG Group acts as a label or tag that can be applied to individual test methods, or even entire classes.

  • The Goal: To logically organize a large, heterogeneous set of tests into manageable categories.
  • The Mechanism: A test method is simply assigned one or more group names (e.g., "Smoke," "Regression," "Database," "UI"). A single test can belong to multiple groups.
  • Benefit: This creates a many-to-many relationship where tests are tagged based on their function, scope, or execution time.


2. Selective Execution Theory 

The primary utility of groups is to control which tests are executed during a specific test run. This control is managed through the testng.xml configuration file.

  • Inclusion: You can theoretically instruct the TestNG runner to include specific groups. For example, by specifying the "Sanity" group, the runner will only execute methods and classes tagged with "Sanity," skipping all others. This is essential for quickly verifying critical functionality.
  • Exclusion: You can also instruct the runner to exclude certain groups. For instance, you might run a full regression suite but explicitly exclude the "LongRunning" group to save time, or exclude the "Database" group if the database is temporarily unavailable.


3. Execution Scope and Filtering

Groups allow for granular filtering at different levels of the TestNG hierarchy.

  • Method Filtering: The most common use is to tag and filter individual test methods.
  • Class/Package Filtering: A group can be applied at the class or package level, meaning every test method inside that container automatically inherits that group tag. This simplifies organization when dealing with large modules.
  • Pre-defined Execution: By combining inclusion and exclusion rules in the testng.xml, a single test class file can support multiple, pre-defined test runs (e.g., a "CI-Build" run that uses the "Smoke" group, and a "Nightly" run that uses the "Regression" group).












Why Use Groups?

  • Run a subset of tests without running the entire suite
  • Reuse the same test in multiple groupings
  • Organize tests based on functionality, feature, or type (example: @smoke, @regression, @login)


Example: TestNG Groups using Java

Step 1: Create Java Test Class with Groups

Below in Test class, we have created Tests which are annotated with @Test and created groups with groups attribute.(ex: groups = {"smoke, "login"}
We can name multiple groups in groups attribute separated with comma. 

import org.testng.annotations.Test;

public class GroupTestExample {

    @Test(groups = {"smoke", "login"})
    public void loginTest() {
        System.out.println("Smoke/Login Test: Login functionality");
    }

    @Test(groups = {"regression"})
    public void profileTest() {
        System.out.println("Regression Test: Profile page validation");
    }

    @Test(groups = {"sanity", "checkout"})
    public void paymentTest() {
        System.out.println("Sanity/Checkout Test: Payment flow");
    }

    @Test
    public void generalTest() {
        System.out.println("General Test: No group assigned");
    }
}



Step 2: Define Groups in testng.xml

Below is the testng.xml file, here only smoke group is included as shown below so tests which is in smoke group will be executed. In above java class only one test is there which has smoke group, so only that test will execute.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="GroupSuite">
  <test name="SmokeGroupOnly">
    <groups>
      <run>
        <include name="smoke"/>
      </run>
    </groups>
    <classes>
      <class name="GroupTestExample"/>
    </classes>
  </test>
</suite>



Output

Smoke/Login Test: Login functionality


We can also run multiple groups:

<include name="smoke"/>
<include name="sanity"/>



We can also exclude specific groups:

<groups>
  <run>
    <exclude name="regression"/>
  </run>
</groups>



If a test case execution depends upon successful execution of group of test cases then we can also create dependencies on test cases by using 'dependsOnGroups' attribute in @Test annotation, as shown below in code snippet.

@Test(groups = "db")
public void connectToDB() {
    System.out.println("Connected to DB");
}

@Test(groups = "sanity", dependsOnGroups = "db")
public void fetchUserData() {
    System.out.println("Fetched user data after DB connection");
}



Suggested Posts:

1. Priority in TestNG
2. TestNG Annotations
3. TestNG Asserts
4. First Script in TestNG
5. Read Excel Data by Data provider

Introduction to TestNG

   


What is TDD?

Test-Driven Development (TDD) is a software development process that relies on the repetition of a very short development cycle: Red, Green, Refactor. It is more a design technique than a testing technique.


What is TestNG?

TestNG stands for Test Next Generation. It is an open-source testing framework inspired by JUnit and NUnit, designed specifically for the Java programming language. It overcomes the limitations of older frameworks by introducing powerful features for unit testing, functional testing, integration testing, and end-to-end testing.

TestNG's centers on providing a powerful, flexible, and structured environment for running tests. It addresses the limitations of older frameworks by offering features that make writing and managing complex test scenarios easier.


1. Annotation-Driven Structure

Core Concept: Tests are structured using annotations (special markers) that tell the framework what a method is, when it should be run, and how it relates to other methods.

Example: Annotations can mark a method as the setup code to run before an entire test class, the cleanup code to run after a test method, or the test method itself. This provides a clear, logical structure for setting up and tearing down testing environments.


2. Parallel Execution and Grouping

Core Concept: TestNG can run tests in parallel (concurrently), which significantly reduces the total execution time of a large test suite.

Grouping: It allows tests to be assigned to groups (example: "sanity," "regression," "database"). A developer can then choose to run only a specific group of tests, providing granular control over the testing scope.


3. Data-Driven Testing (DDT)

Core Concept: TestNG supports running the same test method multiple times with different sets of input data.

Mechanism: It uses Data Providers to supply various parameters to the test method, efficiently testing a function across many possible inputs without duplicating the test logic.


4. Dependency Management

Core Concept: Tests can be declared as dependent on the successful completion of other test methods.

Benefit: If a setup test (like a login process) fails, TestNG can be configured to skip dependent tests, saving time and clearly indicating the root cause of the failure.


TestNG's Impact:

  • Flexibility: It moves beyond simple unit testing to handle complex integration and system tests.
  • Reporting: It generates detailed, customizable HTML reports, making it easier to analyze test results and identify failures.
  • Scalability: Its features like parallel execution and grouping make it suitable for managing very large and complex test automation projects.












Below are some key features of Testng

  • Annotations:
    TestNG uses a wide variety of annotations (@Test, @BeforeClass, @AfterMethod, etc.) to control test behavior and execution flow.
  • Flexible Test Configuration:
    Allows grouping, prioritization, dependencies, and conditional test execution.
  • Parallel Test Execution:
    You can run multiple tests simultaneously, which reduces total execution time and is useful for large projects.
  • Parameterization:
    TestNG supports both static and dynamic test data through parameters and data providers.
  • Test Suite Execution:
    Tests can be organized into suites using an XML configuration file (testng.xml), allowing you to control test runs from a central place.
  • Powerful Reporting:
    It automatically generates HTML and XML reports after execution, providing insights into test pass/fail status.
  • Integration Support:
    Works well with tools like Maven, Jenkins, Selenium WebDriver, Eclipse, IntelliJ, and various CI/CD tools.
  • Exception Handling:
    It allows defining what exceptions to expect, which helps in testing negative or error-prone scenarios.

Structure of TestNG Testing

  • Test Suite:
    A collection of tests defined in the testng.xml file.
  • Test:
    A test block within a suite. It can include multiple test classes.
  • Test Class:
    Java class containing test methods and configurations.
  • Test Methods:
    Individual test cases.

Test Execution Flow in TestNG

TestNG follows a specific sequence in running methods:

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod
  • Test Method
  • AfterMethod
  • AfterClass
  • AfterTest
  • AfterSuite

Types of Testing Supported by TestNG

  • Unit Testing: Test individual components.

  • Functional Testing: Validate business logic.

  • Integration Testing: Test combined modules.

  • Regression Testing: Re-run test cases to ensure no new bugs.

  • Data-Driven Testing: Run same tests with different inputs.


We can use Testng for below scenarios

  • Automating browser testing with Selenium

  • Running API tests

  • Validating database operations

  • Managing large test suites

  • Executing tests in a distributed environment


Tools and Environment Compatibility

  • IDEs: Eclipse, IntelliJ IDEA

  • Build Tools: Maven, Gradle

  • CI/CD Tools: Jenkins, Bamboo

  • Reporting Tools: Allure, ExtentReports

  • Testing Tools: Selenium WebDriver, REST Assured



Suggested Posts:

1. TestNG Suite
2. TestNG Dependent Tests
3. TestNG Reports
4. Parameterization in TestNG
5. Priority in TestNG