Catalog / TestNG Cheatsheet
TestNG Cheatsheet
A quick reference guide for TestNG annotations, features, and best practices for Java testing.
Core Annotations
Test Suite Setup/Teardown
|
Executed before all tests in the suite. |
|
Executed after all tests in the suite. |
|
Executed before all tests within a |
|
Executed after all tests within a |
|
Executed before a specific group of tests. |
|
Executed after a specific group of tests. |
Test Class Setup/Teardown
|
Executed before the first test method in the class. |
|
Executed after all test methods in the class have run. |
|
Executed before each test method. |
|
Executed after each test method. |
Test Method
|
Marks a method as a test method. Example: |
|
Defines the execution order of tests within the class. |
|
Skips the test. |
|
Fails the test if it exceeds 1000ms. |
|
Passes the test if the specified exception is thrown. |
Parameterization & Data Providers
Data Providers
|
Marks a method as a data provider. Example:
|
|
Specifies the data provider for a test method. Example:
|
Data providers are a powerful way to parameterize tests and run the same test method with different sets of data. |
Parameters in XML
|
Define parameters in the |
|
Inject parameters defined in the Example:
|
Advanced Features
Groups
|
Assigns a test method to one or more groups. Groups can be used for selective test execution. |
|
Define groups to include or exclude in Example:
|
Dependencies
|
Specifies that a test method depends on other methods. If the dependent methods fail, this method will be skipped. |
|
Specifies that a test method depends on a group of methods. If any method in the group fails, this method will be skipped. |
Listeners
|
Interface for listening to test execution events. Common Methods: |
|
Specifies listeners to be used for test execution. |
Assertions and XML Configuration
Assertions
TestNG provides built-in assertions for validating test results. These methods are typically used within Common Assertions: |
testng.xml Configuration
The Key Elements: |
Example structure of
|