- TestNG - TestNG - vs JUnit
- TestNG - Plug with Eclipse
- TestNG - Plug with ANT
- TestNG - Parallel Execution
- TestNG - Asserts
- TestNG - Annotation Transformers
- TestNG - Test Results
- TestNG - Run JUnit Tests
- TestNG - Parameterized Test
- TestNG - Dependency Test
- TestNG - Exception Test
- TestNG - Group Test
- TestNG - Ignore a Test
- TestNG - Suite Test
- TestNG - Executing Tests
- TestNG - Execution Procedure
- TestNG - Basic Annotations
- TestNG - Writing Tests
- TestNG - Environment
- TestNG - Overview
- TestNG - Home
TestNG Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
TestNG - Basic Annotations
The traditional way to indicate test methods in JUnit 3 is by prefixing their names with test. This is a very effective method for tagging certain methods in a class as having a special meaning, but the naming doesn’t scale very well (what if we want to add more tags for different frameworks?) and is rather inflexible (what if we want to pass additional parameters to the testing framework?).
Annotations were formally added to the Java language in JDK 5, and TestNG made the choice to use annotations to annotate test classes.
Here is the pst of annotations that TestNG supports −
Sr.No. | Annotation & Description |
---|---|
1 |
The annotated method will be run only once before all tests in this suite have run. |
2 |
The annotated method will be run only once after all tests in this suite have run. |
3 |
The annotated method will be run only once before the first test method in the current class is invoked. |
4 |
The annotated method will be run only once after all the test methods in the current class have run. |
5 |
The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. |
6 |
The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. |
7 |
The pst 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. |
8 |
The pst 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. |
9 |
The annotated method will be run before each test method. |
10 |
The annotated method will be run after each test method. |
11 |
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 pst 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. |
12 |
Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ]. |
13 |
Defines psteners on a test class. |
14 |
Describes how to pass parameters to a @Test method. |
15 |
Marks a class or a method as a part of the test. |
Benefits of Using Annotations
Following are some of the benefits of using annotations −
TestNG identifies the methods it is interested in, by looking up annotations. Hence, method names are not restricted to any pattern or format.
We can pass additional parameters to annotations.
Annotations are strongly typed, so the compiler will flag any mistakes right away.
Test classes no longer need to extend anything (such as TestCase, for JUnit 3).