TestNG Interview Questions and Answers | Top 31+ TestNG Interview Questions

TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities

Question #1 :What is TestNG?

TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities

Question #2 : Advantages of TestNG?

Question #3 : Annotations of TestNG?

  • @BeforeTest
  • @AfterTest
  • @BeforeClass
  • @AfterClass
  • @BeforeMethod
  • @AfterMethod
  • @BeforeSuite
  • @AfterSuite
  • @BeforeGroups
  • @AfterGroups
  • @Test

Question #4 :  What is Testng.xml file?

 

  • Testng.xml file allows to include or exclude the execution of test methods and test groups
  • It allows to pass parameters to the test cases
  • Allows to add group dependencies
  • Allows to add priorities to the test cases
  • Allows to configure parallel execution of test cases
  • Allows to parameterize the test cases

Question #5 :  How to Pass Parameter Through Testng.xml File to a Test Case?

public class ParameterizedTest {

 @Test

 @Parameters("browser")

 public void parameterizedTest(String browser){

if(browser.equals("firefox")){

  System.out.println("Open Firefox Driver");

 }

else if(browser.equals("chrome"))

{

  System.out.println("Open Chrome Driver");

        }

 } 

}

 

<parameter name=”browser” value=”firefox”/>

Question # 6:  List some TestNG Assert and list out common TestNG Assertions

  • assertEqual(String actual,String expected)
  • assertEqual(String actual,String expected, String message)
  • assertEquals(boolean actual,boolean expected)
  • assertTrue(condition)
  • assertTrue(condition, message)
  • assertFalse(condition)
  • assertFalse(condition, message)

Question # 7:   What is Soft Assert in TestNG?

Soft Assert does not throw an exception when an assert fails and would continue with the next step after the assert statement.

import org.testng.asserts.SoftAssert;

 

public class SoftAssertion {

 

 @Test

 public void softAssert(){

 SoftAssert softAssertion= new SoftAssert();

 System.out.println("softAssert Method Was Started");

 softAssertion.assertTrue(false);

 System.out.println("softAssert Method Was Executed");

 }

Question # 8:   What is Hard Assert in TestNG?

Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test

 

 

public class HardAssertion {

 

 public void hardAssert(){

 System.out.println("hardAssert Method Was Started");

 Assert.assertTrue(false);

 System.out.println("hardAssert Method Was Executed");

 }

Question # 9:  How to create Group of Groups in TestNG?

<groups>
   <define name="all">
	 <include name="smokeTest"/>
	 <include name="functionalTest"/>
   </define>
   <run>
         <include name="all" />
   </run>        
</groups>

Question # 10:  How to set test case priority in TestNG?

package TestNG;
import org.testng.annotations.*;
public class PriorityTestCase{
 @Test(priority=0)
 public void testCase1() {  
 system.out.println("Test Case 1");
 }
 @Test(priority=1)
 public void testCase2() { 
 system.out.println("Test Case 2");
 }
}

 

 

 

2 thoughts on “TestNG Interview Questions and Answers | Top 31+ TestNG Interview Questions”

Leave a Comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.

🛑 Early Bird Discount : Exact Blueprint to Become SDET.Enroll now. (50% OFF),After $299
+ +

Where can we send your free guide?

0 Shares
Share
Tweet
Pin