Protected by Copyscape
Powered By Blogger

Thursday, January 16, 2020

What is TestNG? Why & how we can use it with Selenium?

TestNG stands for "Test Next Generation". TestNG is a framework which was designed by Cedric Beust and it covers all types of testing like Unit, Integration, functional and end to end testing.

In real time scenario if you have to automate 10 business scenarios then might be you would have the following expectations:

1. Test Framework should be user friendly and easy to understand.
2. We should have the facility of grouping of the test cases like for Smoke and Regression Testing.
3. We should get to know instantly that how many test cases passed or failed or skipped.
4. We should have the provision to execute the failed test cases separately.
5. We should have something that at same time we can run our test cases on multiple browsers i.e. Parallel testing.
6. After completion of the execution test report should be generated in an interactive format which can be used to showcase the execution of test report.

All of the above expectations we can achieve by using TestNG framework. In TestNG framework, there is no main method instead we use below annotations which defines the flow of execution. We always put '@' symbol before any TestNG annotation.

Please find below annotations along with description which we implement while using TestNG framework.

@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.
@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: Define listeners on a test class.
@Parameters: Define how to pass parameters to a @Test method.
@Test: Marks a class or a method as part of the test.


@Test is the most commonly used annotation as through this you define your each test case. Without this annotation your program will not run. Other annotations are equally important and gradually you will get to know about the use of it.

I am taking one scenario so that you can understand when you should use which annotation. There is a website which contains employee data like employee's id, salary, position, age etc. We have to validate that we should do login in to the application, enter new employee data and validate that employee data has stored in the database successfully and check the existing employee data and then do log-off from the application. This scenario contains mainly 3 below flows
  • Launch employee web site in any browser and do login.
  • Enter new Employee data and validate
  • Fetch existing employee data and validate.
Above each flow we can count as a test case where we can use @Test annotation. But before execution of the above 3 test cases we have to make sure that database connection should get established first and after execution of the 3 test cases we have to close the data base connection. To achieve this task we will use @BeforeTest @AfterTest annotations. 

See the below example.

package Test;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class FirstTestNG 
{
  @BeforeTest
  public void makeDataBaseConnection()
  {
  System.out.println("Database connection established successfully");
  }
  @AfterTest
  public void closeDataBaseConnection()
  {
  System.out.println("Database connection closed successfully");
  }
  
  @Test(priority=1)
  public void doLogin()
  {
  System.out.println("Login done successfully");
  }
  
  @Test(priority=2)
  public void ValidateNewEmployeeData()
  {
  System.out.println("New Employee's data validated successfully");
  }
  
  @Test(priority=3)
  public void ValidateExistingEmployeeData()
  {
  System.out.println("Existing Employee's data validated successfully");
  }
}


Below is the output of the above code after execution and do notice that in outcome database connection established before execution of any test case and closed after done the execution of all the test cases are over.
*********************************************************************************

[RemoteTestNG] detected TestNG version 6.14.3
Database connection established successfully
Login done successfully
New Employee's data validated successfully
Existing Employee's data validated successfully
Database connection closed successfully
PASSED: doLogin
PASSED: ValidateNewEmployeeData
PASSED: ValidateExistingEmployeeData

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


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

*********************************************************************************


While using annotations we use pre-defined parameters like in the above example I have used 'priority' as a parameter, which tells that which @Test method will be executed first and lowest priority value will be executed first like @Test(priority=1) will be executed first and then so on. Execution done will be based on the alphabetic order if we don't define the priority of the @Test method. Now, I am removing the priority parameter from my test and then see the outcome:

*********************************************************************************

[RemoteTestNG] detected TestNG version 6.14.3
Database connection established successfully
Existing Employee's data validated successfully
New Employee's data validated successfully
Login done successfully
Database connection closed successfully
PASSED: ValidateExistingEmployeeData
PASSED: ValidateNewEmployeeData
PASSED: doLogin

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


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

*********************************************************************************

2 comments:

Ganesh joshi said...

Thanks for sharing helpful content.

Vikas Sachdeva said...

Thanks Ganesh