Tuesday, October 4, 2016

Annotations in Selenium TestNG, Test Case Grouping and Dependent Test Case

In this blog, I am going to focus on the importance of different types of annotations and their usage.


Before writing test scripts or setting up a project, we should know the hierarchy in which the annotations work. The execution will always remain the same.


For example, compile and run the below script and notice the execution order. It will be as following:
  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod
  • Test Case 1
  • AfterMethod
  • BeforeMethod
  • Test Case 2
  • AfterMethod
  • AfterClass
  • AfterTest
  • AfterSuite
Let's consider below example -

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 {
@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");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("This will execute before every Method");
}
@AfterMethod
public void afterMethod() {
System.out.println("This will execute after every Method");
}
@BeforeClass
public void beforeClass() {
System.out.println("This will execute before the Class");
}
@AfterClass
public void afterClass() {
System.out.println("This will execute after the Class");
}
@BeforeTest
public void beforeTest() {
System.out.println("This will execute before the Test");
}
@AfterTest
public void afterTest() {
System.out.println("This will execute after the Test");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("This will execute before the Test Suite");
}
@AfterSuite
public void afterSuite() {
System.out.println("This will execute after the Test Suite");
}
}
Output of the above code will be like this:


It is clearly visible that the @Suite annotation is the very first and the very lastly executed. Then @Test followed by @Class. Now if you notice, the @Method has executed twice. As @Test is a method in the class, hence @Method will always executed for each @Test method.

Test Case Grouping in TestNG

Group test is a new innovative feature in TestNG, which doesn’t exist in JUnit framework. It permits you to dispatch methods into proper portions and perform sophisticated groupings of test methods. Not only can you declare those methods that belong to groups, but you can also specify groups that contain other groups. Then, TestNG can be invoked and asked to include a certain set of groups (or regular expressions), while excluding another set. Group tests provide maximum flexibility in how you partition your tests and doesn't require you to recompile anything if you want to run two different sets of tests back to back.

Groups are specified in your testng.xml file using the <groups> tag. It can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.

Now, let's take an example to see how group test works.

Create a new TestNG class 'ModuleASuite'. Then create 4 test methods, group tests as 'Regression' and one as 'Smoke Test'. See example below.

package com.example.group;
import org.testng.annotations.Test;
public class groupExamples {
 @Test(groups="Regression")
 public void testCaseOne()
 {
 System.out.println("Im in testCaseOne - And in Regression Group");
 }
 @Test(groups="Regression")
 public void testCaseTwo(){
 System.out.println("Im in testCaseTwo - And in Regression Group");
 }
 @Test(groups="Smoke Test")
 public void testCaseThree(){
 System.out.println("Im in testCaseThree - And in Smoke Test Group");
 }
 @Test(groups="Regression")
 public void testCaseFour(){
 System.out.println("Im in testCaseFour - And in Regression Group");
 }
}

 Now, Update the TestNG.xml file. We will execute the group “Regression” which will execute the test methods which are defined with group as “Regression”

<suite name = "Test - Suite">
    <test name="website QA">
        <groups>
            <run>
            <include name="Regression"/>
            </run>
        </groups>
        <classes>
            <class name="automationFramework.ModuleASuite" />
        </classes>
    </test>
</suite>


Run TestNG.xml and the output will be like this -







Dependent Test Case


Sometimes, 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 as it supports the declaration of explicit dependencies between test methods.
TestNG allows you to specify dependencies either with:
  • Using attributes dependsOnMethods in @Test annotations OR
  • Using attributes dependsOnGroups in @Test annotations.
Take a look over the below example:

package automationFramework;

import org.testng.annotations.Test;

public class ModuleBSuite {

      @Test (dependsOnMethods = { "OpenBrowser" })
      public void SignIn() {
          System.out.println("This will execute second (SignIn)");
      }

      @Test
      public void OpenBrowser() {
          System.out.println("This will execute first (Open Browser)");
      }

      @Test (dependsOnMethods = { "SignIn" })
      public void LogOut() {
          System.out.println("This will execute third (Log Out)");
      }
}

Update and run TestNG and the output will be like this - 

<suite name = "Test - Suite">
    <test name="website QA">
        <classes>
            <class name="automationFramework.ModuleBSuite" />
        </classes>
    </test>
</suite>





 

No comments:

Post a Comment