Thursday, 14 June 2018

TestNG Grouping and Depends On

In our previous post we have learnt about TestNG annotations . In this post we will see how we can group test cases and specify dependent test cases using TestNG annotations.

Groups‘ is one annotation of TestNG which can be used in the execution of multiple tests as a group. Let’s say you have hundred tests of class Devices and in it ten methods of laptops, ten method of Notebooks and so on. You probably like to run all the Notebooks tests together in a batch. And you want all to be in a single test suite. With the help of grouping you can efficiently address this situation.

How to do it…
1) Create two methods for Laptops, two methods for Notebooks and one method in conjunction with Laptops and Notebooks.
2) Group them separately with using  (groups = { ” Group Name” })


import org.testng.annotations.Test;
 
public class TestGrouping {
 
  @Test (groups = { "Laptops" })
 
  public void Car1() {
 
   System.out.println("Batch Laptops- Test Laptops1");
 
  }
 
  @Test (groups = { "Laptops" })
 
  public void Car2() {
 
   System.out.println("Batch Laptops- Test Laptops 2");
 
  }
 
  @Test (groups = { "Notebooks" })
 
  public void Scooter1() {
 
   System.out.println("Batch Notebooks- Test Notebooks 1");
 
  }
 
  @Test (groups = { "Notebooks" })
 
  public void Scooter2() {
 
   System.out.println("Batch Notebooks- Test Notebooks2");
 
  }
 
  @Test (groups = { "Laptops", "Touch Screen Laptops" })
 
  public void Sedan1() {
 
   System.out.println("Batch Touch Screen Laptops- Test Touch Screen Laptops1");
 
  }
 
}

Add below code into TestNG.xml-



<suite name="Suite">
 
    <test name="Practice Grouping">
 
        <groups>
 
     <run>
 
  <include name="Laptops" />
 
     </run>
 
 </groups>
 
 <classes>
 
     <class name="PackageName.TestGrouping" />
 
 </classes>
 
    </test>
 
</suite>

Run The TestNG,xml -> right click -> Run as -> TestNG Suit.

Dependenct Test -

you may need to invoke methods in a Test case in a particular order or you want to share some data and state between methods. This kind of dependency is supported by TestNG .
TestNG allows you to specify dependencies with two options given below.
  • Using attributes dependsOnMethods in @Test annotations
  • Using attributes dependsOnGroups in @Test annotations.

mport org.testng.annotations.Test;
 
public class Dependent {
 
  @Test (dependsOnMethods = { "OpenBrowser" })
 
  public void SignIn() {
 
   System.out.println("This will execute SignIn");
 
  }
 
  @Test
 
  public void OpenBrowser() {
 
   System.out.println("This will execute Open Browser");
 
  }
 
  @Test (dependsOnMethods = { "SignIn" })
 
  public void SignOut() {
 
   System.out.println("This will execute Log Out");
 
  }

In case of any feedback and questions post in the comment section below.

What is TestNG Annotations

Hi everyone in this post we will learn basics of TestNG annotations. In our previous tutorial we have seen introduction and setup of TextNG. Now lets learn what is TestNG annotations and how to use TestNG annotations to define proper test suit execution flow.


TestNG Annotation

Lets understand the simple example given below which will help you understand how annotations helps to define the flow.In TestNG annotations @Test is the smallest annotation here. @Method will be executed first, before and after the execution of @Test. The same way @Class will be executed first, before and after the execution of @Method and so on.

Do try this out and post your feedback and questions in the comment section below.


import org.testng.annotations.AfterClass;
 
import org.testng.annotations.AfterMethod;
 
import org.testng.annotations.AfterSuite;
 
import org.testng.annotations.AfterTest;
 
import org.testng.annotations.BeforeClass;
 
import org.testng.annotations.BeforeMethod;
 
import org.testng.annotations.BeforeSuite;
 
import org.testng.annotations.BeforeTest;
 
import org.testng.annotations.Test;
 
public class Sequencing { 
 
               @BeforeSuite
 
  public void beforeSuite() {
 
   System.out.println("This will execute before the Test Suite");
 
  }
  
 
                @BeforeClass
 
  public void beforeClass() {
 
   System.out.println("This will execute before the Class");
 
  } 
               @BeforeTest
 
  public void beforeTest() {
 
   System.out.println("This will execute before the Test");
 
  }
 
                @BeforeMethod
 
  public void beforeMethod() {
 
   System.out.println("This will execute before every Method");
 
  } @Test
 
  public void testCase1() {
 
   System.out.println("This is the Test Case 1");
 
  }
 
  @Test
 
  public void testCase2() {
 
   System.out.println("This is the Test Case 2");
 
  }
 
  
 
  @AfterMethod
 
  public void afterMethod() {
 
   System.out.println("This will execute after every Method");
 
  }
 
  
 
  @AfterClass
 
  public void afterClass() {
 
   System.out.println("This will execute after the Class");
 
  }
 
  
 
  @AfterTest
 
  public void afterTest() {
 
   System.out.println("This will execute after the Test");
 
  }
 
  
  @AfterSuite
 
  public void afterSuite() {
 
   System.out.println("This will execute after the Test Suite");
 
  }
 
 }

Saturday, 9 June 2018

Introduction to TestNG and TestNG Setup in Eclipse

TestNG is Next Generation Testing framework developed in the lines of JUnit and NUnit, but it has some new functionalities that make it more powerful and easier to use. TestNG is designed to cover all kinds of tests: unit, functional, end-to-end, integration, etc., and it requires JDK 5 or higher.


Pros of using TestNG -
 
There are number of benefits but from Selenium/Appium perspective, major advantages of TestNG are :
  1. It gives the ability to produce HTML Reports of Test Suit execution.
  2. Annotations makes testers life easy and defines flow of test execution.
  3. Test cases can be Grouped & Prioritized more easily using TestNG annotations.
  4. Parallel testing is possible for example running same test on multiple browsers at the same time.
  5. Generates Logs which are useful for debugging purposes.
  6. Data Parameterization is possible.


 How to Configure TestNG in Eclipse -

1. Open Eclipse.
2. Navigate to Eclipse Marketplace.
 
3. Search TestNG.

 
4. Click on Install.
5. Done.