January 19

0  comments

 1. What is TestNG

TestNG is an automation testing framework. TestNG is designed based on JUnit however many disadvantages of JUnit are addressed which makes TestNG unique and easy for an end to end Testing.

2. Mention some of the Advantages of TestNG?

  • Easy to group the test cases
  • Easy to Configure multi-browser testing
  • Easy to integrate with other frameworks such as Maven, Jenkins
  • Comes with built-in HTML reports
  • Allows to prioritize test cases
  • Provides the option to parameterize test cases
  • Provides the option to perform Data-driven testing
  •  Easy to implement assertions

3. Explain the difference between Test and Test Suite in TestNG.

  • TestNG Test is a single test file or test case.
  • TestNG Test Suite is a collection of tests that can be configured in TestNG using .xml file.
  • A suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.
  • A test is represented by <test> and can contain one or more TestNG classes.
  • A TestNG class is a Java class that contains at least one TestNG annotation. It is represented by the <class> tag and can contain one or more test methods.
  • A test method is a Java method annotated by @Test in your source.
  • testng.xml example

< <suite name = "Suite Name">

   <test name = "Test Name">

      <classes>

         <class name = "Test Class Name" />

      </classes>

   </test>

</suite>

4. Why should we use TestNG in Selenium Framework?

Selenium provides API to simulate browser actions, it doesn’t provide assertions, reporters, and execution configuration. Using Selenium with TestNG makes complete Automation Framework.

5. How to add TestNG maven dependency in POM.xml?

Open pom.XML in your maven project, you need to simply add the below entries to POM.xml file.

 <dependency>

  <groupId>org.testng</groupId>

  <artifactId>testng</artifactId>

  <version>6.8</version>

  <scope>test</scope>

</dependency>

6. What is TestNG.XML File?

  • Testing.xml file helps us to configure our test execution behaviour. Such as.
  • Running Multiple Tests
  • Include and Exclude Test cases
  • Adding Dependancies
  • Parameterization of Test cases
  •  Parallel Execution

7. What is annotation in TestNG?

The TestNG annotations are used to control the flow of execution in test script. TestNG annotations are used in combination with methods or classes.

8. List some of the most common annotations used in TestNG.

  • Annotations
  • Description
  • @BeforeSuite
  • The @BeforeSuite method in TestNG runs before the execution of all other test methods.
  • @AfterSuite
  • The @AfterSuite method in TestNG runs after the execution of all other test methods.
  • @BeforeTest
  • The @BeforeTest method in TestNG runs before the execution of all the test methods that are inside that folder.
  • @AfterTest
  • The @AfterTest method in TestNG executes after the execution of all the test methods that are inside that folder.
  • @BeforeClass
  • The @BeforeClass method in TestNG will run before the first method invokes the current class.
  • @AfterClass
  • The @AfterClass method in TestNG will execute after all the test methods of the current class execute.
  • @BeforeMethod
  • The @BeforeMethod method in TestNG will execute before each test method.
  • @AfterMethod
  • The @AfterMethod method in TestNG will run after each test method is executed.
  • @BeforeGroups
  • The @BeforeGroups method in TestNG runs before the test cases of that group are executed. It executes just once.
  • @AfterGroups
  • The @AfterGroups method in TestNG runs after the test cases of that group are executed. It executes only once.

9. What are PreCondition, Test, and PostCondition Annotations in TestNG?

  • Precondition annotations are those annotations of TestNG that are executed before the execution of test methods The Precondition annotations are @BeforeSuite, @BeforeClass, @BeforeTest, @BeforeMethod
  • Test annotation Methods annotated with @Test are called test methods which serve as a unit test
  • Postcondition annotations are those annotations that are executed after the execution of all the test methods. The postcondition annotation can be @AfterSuite, @AfterClass, @AfterTest, @AfterMethod.

10. What is parameterization in TestNG?

  • Parameterized tests allow the user to run the same test with multiple times but using different values.
  • Example:
  • Test Script

public class ParameterizedTest {

   @Test

   @Parameters("fruit")

   public void parameterTest(String fruit) {

      System.out.println("Parameterized value is : " + fruit);

   }

}

  • testng.xml file

<suite name = "MySuite">

   <test name = "Mytest">

      <parameter name = "fruit" value="apple"/>

      <classes>

         <class name = "ParameterizedTest" />

      </classes>

   </test>

</suite>

11. What are the Priorities in TestNG?

Prioritization in TestNG provides execution sequence to the methods so that they can run on order. One after the other in the specified format.

@Test (priority = 0)

public void myMethod1(){

   //test code

}

@Test (priority = 10)

public void myMethod2(){

   //test code

}

12. How to Define Grouping in TestNG?

  • Grouping in TestNG lets you group multiple test cases in named groups. Once you define the group you can run single or multiple groups.
  • Example
  • Test Script

public class Test

{

  @Test(groups = { "group1", "group2" })

  public void method1()

  {

    //Code

  }

 

  @Test(groups = {"group2"} )

  public void method2()

  {

    //Code

  }

 

  @Test(groups = {"group1"})

  public void method3()

  {

    //Code

  }

}

  • testng.xml file

<suite name="TestNG Grouping">

    <test verbose="2" preserve-order="true">

        <groups>

            <run>

              <include name = "group1"></include>

              <include name = "group2"></include>

            </run>

        </groups>

        <classes>

            <class name="Project.Test"></class>

        </classes>

    </test>

</suite>

13. How can we exclude a group in TestNG?

  • TestNG allows us to ignore tests. This can be used using testng.xml file.
  • If we want to exclude the test group with name “Example” we can do it like below.

<suite name="Test Suite" >

   <test name="Test" >

      <groups>

            <run>

                  <exclude name = "Example">

                  </exclude>

            </run>

      </groups>

       <classes>

          <class name="TestClass" />

       </classes>

   </test>

 </suite>

14. What are assertions in TestNG?

  • TestNG  Assertions help to verify the expected result and the actual result. By verifying the result we can pass/fail the tests.
  • Syntax:
  • Assert.Method(actual, expected, message)
  • There are two types of Asserts in TestNG
  • Hard Assert in TestNG throws an AssertException, when an assert statement fails and the test suite continues with the next @Test method.
  • Soft Assert in TestNG does not throw an exception when an assert fails and would continue with the next step after the assert statement. However, we can collect the error logs and which can be verified later

15. List down common Assertions used in TestNG.

  • assertEqual(actual,expected,message)
  • assertEquals(actual,expected,message)
  • assertNotEquals(actual, expected, message);
  • assertTrue(condition)
  • assertTrue(condition, message)
  • assertFalse(condition)
  • assertFalse(condition, message)
  • assertNull(object);
  • assertNotNull(object);
  • assertNotSame(Object actual, Object expected)
  • assertSame(Object actual, Object expected)

16. Can you explain about handling dependent test Execution in TestNG?

  • A single or group of tests depends on one or more tests are called dependent tests.
  • For example, test1 is depending test2. These scenarios can be handled in TestNg using dependsOnMethods  or dependsOnGroups attributes are used for handling dependencies of tests in TestNG.

public class DependencyTest {

  @Test (dependsOnMethods = { "Test2" })

  public void Test1() {

        System.out.println("Executes After Test2");

  }

  @Test

  public void Test2() {

        System.out.println("Executes First");

  }

}

Note: A similar way dependsOnGroups can be used to define the dependency for groups.

17. How to handle Parallel execution in TestNG?

  • Parallel Testing in TestNG is a technique in which multiple tests are executed simultaneously in different threads to reduce execution time.
  • The testng.xml parallel attribute is used for this purpose.
  • TestNG allows us to execute tests parallelly at various levels.
  • Method: Runs the parallel tests on all @Test methods in TestNG
  • Ex: <suite name = "Parallel Test" parallel = "methods">
  • Test: All the test cases present inside the <test> tag will run with this value.
  • Ex: <suite name="Test class Suite" parallel="tests" thread-count="2">
  • Classes:  Each class will be started and executed simultaneously in different threads
  • Ex: <suite name="Parallel" parallel="classes" thread-count="2">
  • Instances: TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads
  • Ex: <suite name = “Parallel Test Suite” parallel = “instances” thread-count = “4”>

18. How to Define TimeOut in TestNG?

  • TestNG allows users to configure a time for a test to completely execute.
  • Timeout attribute with @Test tag is used for this purpose.
  • Example:

@Test ( timeOut = 500 )

Public void timeOutExample(){

//code

}

  • The timeout can also be handled using the testng.xml file.
  • Example:
  • <suite name= "Test Suite" time-out="500">
  • <test name="Test1" verbose="2" time-out="300">

19. What is TestNG Listners?

  • TestNG Listeners are TestNG annotations that “listen” to the events in a script. This helps to modify the default behavior of TestNG. If we want to take a screenshot of test failures we need to Listen to Test Failure. @Listeners annotations are used at the class level to implement listeners.
  • The ITestListener is the most used TestNG interface it has a method like onTestStart, onTestSuccess, onTestFailure, onTestSkipped, etc. using this we can configure reporters and custom logging capability in TestNG.

20. Tell me about Data Providers in TestNG.

  • TestNG Data Provider is a method of a class which is having annotation @DataProvider and returns an array of objects, which can be used inside our actual test.
  • Example:

public class DataProviderClass {

      @Test (dataProvider="getData")

      public void loginTest(String FirstName, String LastName){

            System.out.println("First Name is "+ FirstName);

            System.out.println("Last Name is "+ LastName);

      }

     

      @DataProvider(name="getData")

      public Object[][] getData(){

            Object [][] data = new Object [2][2];

           

            data [0][0] = "FirstName1";

            data [0][1] = "LastName1";

           

            data[1][0] = "FirstName2";

            data[1][1] = "LastName2";

           

            return data;

           

      }

}

21. How Factory Different from DataProvides?

  • @DataProvider – A test method that uses @DataProvider will be executed multiple times based on the configuration. The test method will be executed using the same instance of the test class to which the test method belongs.
  • @Factory – A factory will execute all the test methods present inside a test class using separate instances of the class.

22. Name the Interface which can be used to retry failed tests?         

IRetryAnalyzer is used to retry failed tests.

Steps to Create Retry Failed Tests

  • Create a class to implement IRetryAnalyzer
  • Create another class ‘RetryListenerClass’ by Implementing the‘ IAnnotationTransaformer’ interface, which will use the implementation of the above class
  • Add Listener to testng.xml File

23. What is the difference between TestNG and Junit?

  • Junit is mostly focused on Unit Testing, TestNG has rich features for both Unit and end to testing.
  • Generating HTML Reports is a complicated task in JUnit, TestNG provides an easy way to generate HTML reports.
  • Advanced Features such as Dependency Test, Parameterization, Priority setting is sometimes a complex task or we need to follow a workaround to implement these features, TestNG comes with the default set of features to support mentioned features.

24. What is expectedExceptions Attribute in TestNG?

If the exception thrown by the test is part of the the expectedExceptions attribute then the exceptions are ignored and not marked as failure.

@Test(expectedExceptions = { IOException.class }) 

      public void exceptionTestOne() throws Exception {

            throw new IOException();

      }

25. How to disable a test in TestNG? Or How to skip tests in TestNG?

We can disable the test using enabled attribute. Setting enabled =false disables the test.

@Test(enabled = false)

public void Test() {

   System.out.println("Hello World");

}

26. What is Inclusion & Exclusion Groups in TestNG?

A Test Group which is included for test execution is called the Inclusion Group. A Test Group which is excluded from test execution is called Exclusion Group

27. What is the invocation count in TestNG?

invocation count is an attribute, when used it executes the test a specified number of times.

public class MultipletimesRun {

    @Test(invocationCount=5)

    public void run five times(){

        System.out.println("Execute 5 times");

    }

}

  • Author: Ganesh Hegde | LinkedIn
  • Ganesh Hegde is an Experienced SDET/Quality Assurance Engineer. He has expertise in Java, C#, Selenium Webdriver, TestNG, Cypress, Protractor, TestCafe, Playwright, WebdriverIO. He is also a passionate blogger and content writer.



Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}