TestNG

  1. What is TestNG

Ans.   – TestNG is an open source automated frame work used to drive all kinds of testing like Functional, unit, integration, system, regression..etc. NG stands for next generation

  • Its build on the base line of  JUnit and NUnit which were previously used as frame works.
  • TestNG overcomes limitations of JUnit like poor configuration control,static programming model (forces you to recompile unnecessarily),the management of different suites of tests in complex projects can be very tricky..etc

 

Some features of TestNG are listed as follows-

1.Supports annotations.

 

2.Uses OOP concepts like interfaces, inheritance, abstraction, polymorphism..etc

 

3.Allows separation of compile-time test code from run-time configuration/data info.

 

4.Flexible plug-in API.

 

5.Support for multi threaded testing.

 

  1. Grouping of tests.

 

  1. Explain with example

@BeforeSuite

@AfterSuite

@BeforeTest

@AfterTest

@BeforeGroups

@AfterGroups

@BeforeClass

@AfterClass

@BeforeMethod

@AfterMethod

 

Ans.

 

Annotations – Annotations are used in TestNG to schedule the test  methods, i.e. it gives user the facility to determine which test in the test class(test suit) should run first and then the other tests as desired.

 

@BeforeSuit – The annotated method will be run only once before all tests in this suite have run.

 

@AfterSuite – The annotated method will be run only once after all tests in this suite have run.

 

@BeforeTest – The annotated method will be run only once before the first test method in the current class is invoked.

 

@AfterTest – The annotated method will be run only once after all the test methods in the current class 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 only once before the first test method in the current class is invoked.

 

@AfterClass – The annotated method will be run only once after all the test methods in the current class have run.

 

@BeforeMethod – The annotated method will be run before each test method.

 

@AfterMethod – The annotated method will be run after each test method.

 

For E.g

 

public class TestClass1 {

 

    @BeforeSuite

 

    public void suiteSetup1() {

 

        System.out.println(“testClass1.suiteSetup1: before suite”);

 

    }    

    @BeforeTest

 

    public void beforeTest() {

 

        System.out.println(“testClass1: before test”);

 

    }

 

    @Test

 

    public void unitLevel1() {

 

        System.out.println(“testClass1: Unit level1 testing”);

 

    }

 

    @Test

 

    public void unitLevel2() {

 

        System.out.println(“testClass1: Unit level2 testing”);

 

    }

 

    @BeforeMethod

 

    public void beforeMethod() {

 

        System.out.println(“testClass1: before method”);

 

    }

 

    @AfterMethod

 

    public void afterMethod() {

 

        System.out.println(“testClass1: after method”);

 

    }

 

    @BeforeClass

 

    public void beforeClass() {

 

        System.out.println(“testClass1: before class”);

 

    }

 

    @AfterClass

 

    public void afterClass() {

 

        System.out.println(“testClass1: after class”);

 

    }

 

    @AfterSuite

 

    public void cleanupSuite() {

 

        System.out.println(“testClass1.cleanupSuite: after suite”);

 

    }

 

   }

3.What is priority how it is implemented explain with example

 

Ans – In TestNG “Priority” is used to schedule the test cases. When there are multiple test cases, we want to execute test cases in order. Like First we need to execute a test case “Registration” before login.

 

E.G

 

import org.testng.annotations.Test;
public class testNGPriorityExample {
@Test
public void registerAccount()
{
System.out.println(“First register your account”);
}
@Test(priority=2)
public void sendEmail()
{
System.out.println(“Send email after login”);
}

 

4.What are groups  explain with example

 

Ans. TestNG allows us to perform sophisticated groupings of test methods.

Using TestNG  we can execute only set of groups while excluding another set. This gives us the maximum flexibility in dividing tests and doesn’t require us to recompile anything if you want to run two different sets of tests back to back.

Groups are specified in testng.xml file and can be used either under the or tag. Groups specified in the tag apply to all the tags underneath.

 

E.G

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”);
}

 

  1. How the annotation @parameter is implemented

Ans. TestNG allows the user to pass values to test methods as arguments by using parameter annotations through testng.xml file.

 

package com.parameterization;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestParameters {

@Parameters({ “browser” })
@Test
public void testCaseOne(String browser) {
System.out.println(“browser passed as :- ” + browser);
}

 

  1. Explain textng.xml ,different tags used in it and their significance with example.

 

Ans:-In testng.xml file we can specify multiple name (s) which needs to be executed if any  project

 

there may be many classes, but we want to execute only the selected classes.

The below is the example testng.xml which will execute the specific packages.

 

Example:

<?xml version=”1.0″ encoding=”UTF-8″?>
<suite name=”example suite 1″ verbose=”1″ >
 <test name=”Regression suite 1″ >
<packages>
  <package name=”com.first.example” />

<class name= “sample”>

</class>
  </packages>
</test>
</suite>

 

  • A suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.
  • Tag <test> represents one test and can contain one or more TestNG classes.
  • <class> tag represents a TestNG class. It is a Java class that contains at least one TestNG annotation. It can contain one or more test methods.