TestNG Annotations

TestNG comes with a series of Annotations. TestNG is very rich in annotations. We will see the TestNG annotations with Example.

You don’t need to main() method to execute your Test Class. TestNG annotation will create the object of the class and start execution.

 

Let’s see important annotations of TestNG.

1. @BeforeSuite – Methods attached to this annotation execute before the execution of any single method present in the TestNg Test Suite.

 

2. @AfterSuite – Methods attached to this annotation execute after the execution of all Test methods of it’s Test Suite.

 

3. @BeforeTest – @BeforeTest annotated method will be executed before the any @Test annotated method of those classes which are inside <test> tag in testng.xml file.

 

Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

4. @AfterTest– @AfterTest annotated method will be executed when all @Test annotated methods complete its execution of those classes which are inside <test> tag in testng.xml file.

 

5. @Test – This annotation attached to the test method in the Test Class. You can declare your Test Methods under the @Test annotations.

 

6. @BeforeGroups – BeforeGroups annotated method will run before the first test run of that specific group.

 

7. @AfterGroups – AfterGroups annotated method will run after all test methods of that group completes its execution.

 

8. @BeforeClass – Method annotated using @BeforeClass will be executed before first @Test method execution. @BeforeClass annotated method will be executed once only per class so don’t be confused.

 

9. @AfterClass – Same as @BeforeClass, Method annotated with @AfterClass annotation will be executed once only per class after execution of all @Test annotated methods of that class.

 

10. @Parameters – Parameters annotation is used to pass parameters to your test methods, you need to use @Parameters annotation.

 

Let’s see an example of TestNG annotations.

Output:

Before Suite
Before Test
Before Class
 Before method
Hi, I am from first Test
After Method
 Before method
Hi, I am from second Test
After Method
After Class
After Test
PASSED: firstTest
PASSED: secondTest

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

After Suite

===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

So, you can see the structure of TestNG class and output. We will discuss these all in details in coming tutorials.

0 Comment

Leave a Reply