Saturday, February 6, 2016

TestNG

Posted by: Kiran Thurimerla
 (SQA Module Lead working at Ebix Software India PVT Ltd).
Contact Author: kiran.thurimerla@gmail.com

Introduction: 


The TestNG Eclipse plug-in allows you to run your TestNG tests from Eclipse and easily monitor their execution and their output. TestNG with WebDriver provides an efficient and effective test result format that can in turn be shared with the stake holders to have a glimpse on the product’s/application’s health thereby eliminating the drawback of WebDriver’s incapability to generate test reports. TestNG has an inbuilt exception handling mechanism which lets the program to run without terminating unexpectedly.

Steps to Install TestNG Eclipse Plugin 


1.       Launch the Eclipse IDE.

2.       Click “Help” -> “Install New Software …”.

 
3. Click “Add” button
 
 
4.  Enter Name as “TestNg” and Location as “http://beust.com/eclipse”.
 
5. Now, click “OK” button then “Next >” button.
 
6. Click Finish button by selecting the radio button “I accept the terms of the license agreement” and then click “Finish” button.
7. To check whether TestNG is installed in Eclipse, go “File” menu à “New” à “Other…”
Like JUnit, TestNG is also a testing framework which uses annotations,  but with  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. 
As we discussed above, We will learn how the TestNG with WebDriver provides an efficient and effective test result report.
Let us see a simple program:
The following example, the Java class contains two test cases (methods) “testLogin” and “testRegister”.  
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; 
public class LoginTest {
       @BeforeTest
       public void testOpenConnections() {
              System.out.println("Connectedt to Database");        
       }
    @AfterTest
       public void testCloseConnection() {
              System.out.println("DB connection closed");     
      }
    @BeforeMethod
    public void testOpenBrowser() {
    System.out.println("Browser Open ...");
    }
    @AfterMethod
       public void testCloseBrowser() {
      System.out.println("Browser closed ...");
    }
   @Test
   public void testLogin() { 
    System.out.println("Login Page");
       }
  @Test
  public void testRegister(){
System.out.println("Register Page");
    }
}
For run the above Java Code, Right Click on the Code File à Run as TestNG Test.
 
 
On successful execution, TestNG will create a report folder which will provide the report in Html file
 
 

 
Open the “index.html” file, TestNG test case execution report will be displayed.
 
 

 

Run multiple test cases:

Continuation to  the above example, We will create a test case which test the following test scenario.
i.e., The system should allow to sell the item only if the user logged-in. If the condition fails, then system should skip the method to execute.
Here, we’ll create a class “SellItems.java” which contains the methods “testSellItems()”, “shouldbeSKIP()”  and “isLogedIn()”.
Now, we will run both case test cases sequentially. 
Code:
import org.testng.SkipException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
 
public class SellItems {      
       @BeforeTest
       public void shouldbeSKIP() {
              //check whether the user is loged in or not
              if(!isLogedIn()) {
                     throw new SkipException("Skipping because User is not loged in");
              }
       }
      
       @Test
       public void testSellItems() {
              System.out.println("Sell the Items ... ");
       }
      
       public boolean isLogedIn() {             
              return false;
       }
 
}
 
SellingItems():  System allows the user to sell the items.
isLogedIn():  Cross-checks the user logged in or not.
shouldbeSKIP() : Do not allow the user to user to sell Items if “isLogedIn()” returns “false”. 
For running multiple classes or test cases in sequence, we have to configure the Java classes in an XML. So, create and XML (Ex. TestNG.xml) under project hierarchy.
 
TestNG.xml:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="my sample test">
       <test name="Application Login">
              <classes>
                     <class name="LoginTest"></class>
              </classes>
       </test> 
       <test name="Selling Items">
              <classes>
                     <class name="SellItems"></class>
              </classes>
       </test>
</suite>  
Run the TestNG suite:
 
 
 
 
Step1: “Right Click” on “TestNG.xml” file in Package Explorer.
Step2: Select “Run As”.
Step3: Click “1 TestNG Suite”.
After click on “TestNG Suite”, first LoginTest.java will execute and then SellItems.java will be executed.
REPORT:

 


Results of Running Suit:

 


No comments:

Post a Comment