Creating a TestNG Test Suite allows you to group and run multiple test classes, groups, or methods together using an XML configuration file. This is very useful for organizing your tests logically and controlling test execution.
What is a TestNG Test Suite?
- Logical Grouping: Theoretically, the Suite groups one or more Tests (which are themselves groupings of test classes). It allows you to run a complete, predefined set of tests as a single unit. For instance, an organization might define one Suite for "Daily Smoke Tests" and another for "Full Regression Testing."
- Single Execution Cycle: A Suite defines a single, complete execution cycle. All setup actions defined at the Suite level (@BeforeSuite) run once at the very beginning, and all cleanup actions (@AfterSuite) run once at the very end, regardless of how many individual tests or classes are executed within it.
2. Configuration and Parameter Control
The Suite provides the highest level of external configuration control.
Global Parameters: The Suite level is where you define parameters that are meant to be shared across all the tests, classes, and methods within that Suite. This is ideal for global settings like a base URL, a shared database connection string, or a target environment (e.g., "production" or "staging").
Execution Strategy: It dictates how the tests within it will be run. For example, the Suite defines whether the tests should run sequentially (one after the other) or in parallel (concurrently). This is crucial for managing test execution time and efficiency.
3. Reporting Scope
The Suite defines the boundary for the overall reporting mechanism.
Consolidated Report: When a Suite completes its run, it generates a single, consolidated report detailing the status (pass, fail, skip) of every Test, every class, and every method executed under its definition. This provides a holistic view of the system's quality status from that specific run.
A Test Suite is defined in an XML file generally named as 'testng.xml'. It can:
Include multiple test classes
Run specific methods or groups
Control execution order
Pass parameters to tests
Below are the steps to create a TestNG Suite
Step 1: Create Your Test Classes
import org.testng.annotations.Test; public class LoginTest { @Test public void loginWithValidUser() { System.out.println("Login with valid user executed."); } @Test public void loginWithInvalidUser() { System.out.println("Login with invalid user executed."); } }
import org.testng.annotations.Test; public class HomePageTest { @Test public void checkTitle() { System.out.println("Home page title check executed."); } @Test public void checkProfileButton() { System.out.println("Profile button check executed."); } }
testng.xml Test Suite File<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="MyTestSuite"> <test name="Login Tests"> <classes> <class name="LoginTest"/> </classes> </test> <test name="Home Page Tests"> <classes> <class name="HomePageTest"/> </classes> </test> </suite>
src/test/resources/ for Maven).Step 3: Run the Test Suite
- Right-click on testng.xml
- Select Run As > TestNG Suite
Suggested Posts:
1. Priority in TestNG
2. TestNG Annotations
3. TestNG Asserts
4. Parameterization in TestNG
5. Read Excel Data by Data provider
No comments:
Post a Comment