TestNG: An Open Source Java Testing Framework
This article introduces TestNG.
Overview
TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. TestNG introduces some new functionalities that make it more powerful and easier to use, such as:
- Annotations.
- Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc…).
- Test that your code is multithread safe.
- Flexible test configuration.
- Support for data-driven testing (with
@DataProvider
). - Support for parameters.
- Powerful execution model (no more TestSuite).
- Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc…).
- Embeds BeanShell for further flexibility.
- Default JDK functions for runtime and logging (no dependencies).
- Dependent methods for application server testing.
TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc…
TestNG Annotations
Here is a quick overview of the annotations available in TestNG along with their attributes, refer to TestNG Annotations and Benefits:
Annotations | Description |
---|---|
@BeforeSuite |
The annotated method will be run before all tests in this suite have run. |
@AfterSuite |
The annotated method will be run after all tests in this suite have run. |
@BeforeTest |
The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. |
@AfterTest |
The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. |
@BeforeGroups |
The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. |
@AfterGroups |
The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. |
@BeforeClass |
The annotated method will be run before the first test method in the current class is invoked. |
@AfterClass |
The annotated method will be run after all the test methods in the current class have been run. |
@BeforeMethod |
The annotated method will be run before each test method. |
@AfterMethod |
The annotated method will be run after each test method. Behaviour of annotations in superclass of a TestNG class |
@DataProvider |
Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation. |
@Factory |
Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[] . |
@Listeners |
Defines listeners on a test class. |
@Parameters |
Describes how to pass parameters to a @Test method. |
@Test |
Marks a class or a method as part of the test. |
Execution Sequence of Annotations in TestNG:
References
- TestNG Website
- TestNG Documentation
- TestNG Repo on GitHub
- TestNG Tutorial
- TestNG Beginner’s Guide
- TestNG on Wikipedia
- TestSetup and Evil Static Methods
- JUnit Pain
- Using Annotation Inheritance for Testing