TestNG Interview Questions with Answers - All About Testing TestNG Interview Questions with Answers

Tuesday 3 July 2018

TestNG Interview Questions with Answers

TestNG is the most widely used testing framework in the software industry.  It provides a lot of features over the conventional JUnit framework and is used for different kinds of testing like unit, functional, integration testing etc. 

Question 1: How TestNg is better Junit?

Answer :
          1.TestNG have advance annotations and more annotations then Junit 
          2. Execution pattern can be set in TestNG 
          3. We can concurrently execution our test scripts. 
          4. Test case dependencies can be set in TestNG 
          5.The data-driven framework can be easily implemented using Data Provider. 
          6.TestNG can generate the report in a readable format. 
          7.Grouping of test methods 
  8.Multithreaded execution

Question 2: What is NG stands for in TestNG?

Answer :
NG stands for "next generation" in TestNG.

Question 3 : How suites and tests are configured in TestNG?

Answer :
Suites and tests are configured through XML 
files. By default, the name of the file is testng.xml

Question 4: Why TestNG XML files are used?

Answer :
It allows to include (or exclude) respective packages, classes, and methods in their test suite, used to pass parameters to test methods ,It also allows users to group test methods.

Question 5 : Write an TestNG xml code to execute a particular class.

Answer :
<suite name="First Suite" verbose="1"> 
<test name="First Test" >
<classes>
<class name="test.FirstTest" />
</classes>
</test>
</suite>

Question 6 : How to execute TestNG xml using command line?

Answer :
java -cp "/opt/testng-6.8.jar:bin" org.testng.TestNG testng.xml 

//Write above code in command line 
//-cp option of Java to compiled code 
//org.testng.TestNG consists of the main method 
///opt/testng-6.8.jar is the path to the testng JAR

Question 7 : After execution an HTML report is generated by TestNG in which folder?

Answer :
After execution an HTML report is generated by TestNG in a folder named "test-output".

Question 8: How to execute testng.xml using Eclipse

Answer :
1. Open Eclipse and go to the project where we have created the testng.xml file. 
2. Select the testng.xml file, right-click on it, and select Run As | TestNG suite. 
3. Eclipse will execute the XML file as TestNG suite.

Question 9: Tell Before and After annotations in testNG

Answer :
1.@BeforeSuite/@AfterSuite 
2.@BeforeTest/@AfterTest 
3.@BeforeGroups/@AfterGroups 
4.@BeforeClass/@AfterClass 
5.@BeforeMethod/@AfterMethod

Question 10: Give the name of the attribute which provides method dependency

Answer :
"dependsOnMethods" attribute provides method dependency

Question 11: Give the name of the attribute which provides Groups dependency

Answer :
"dependsOnGroups" attribute provides method dependency

Question 12: What will happen if any method under @Test annotations is set private?

Answer :
If a class is annotated by the Test annotation, TestNG will consider only the methods with public access modifiers as test methods. All the methods with other access modifiers will be neglected by TestNG.

Question 13 : How to disable a test method?

Answer :
Disabling a test can be achieved in TestNG by setting the enable attribute of the Test 
annotation to false 
@Test(enabled=false) 
public void testMethodOne(){ 
System.out.println("Test method one."); 
}

Question 14 : How to set time test at suite level?

Answer :
Add a testng.xml file to the project and put the following code to it: 
<suite name="Time test Suite" time-out="500" verbose="1">
<test name="Timed Test">
<classes>
<class name="test.timetest.TimeSuite" />
</classes>
</test>
</suite>

Question 15: How to set time test at test method level?

Answer :
@Test(timeOut=500) 
public void timeTestOne() throws InterruptedException{ 
Thread.sleep(1000); 
System.out.println("Time test method one"); 
}

Question 16 : How will you create a test that belongs to a group?

Answer :
Test that belong to a group 
public class TestGroup { 
@Test(groups={"test-group"}) 
public void testMethodOne(){ 
System.out.println("Test method one belonging to group."); 
}

Question 17 : List Out Common Testng Assertions?

Answer :
Common Testng Assertions : 
1.assertEqual(String actual,String expected) 
2.assertEqual(String actual,String expected, String message) 
3.assertEquals(boolean actual,boolean expected) 
4.assertTrue(condition) 
5.assertTrue(condition, message) 
6.assertFalse(condition) 
7.assertFalse(condition, message)

Question 18 : What Is Soft Assert In TestNG?

Answer :
Soft Assert collects errors during @Test. Soft Assert does not throw an exception when an assert fails and would continue with execution.

Question 19: What Is Hard Assert In Testng?

Answer :
Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with execution.

Question 20 : How To Set Test Case Priority In Testng?

Answer :
Setting Test Case Priority In Testng 
'@Test(priority=0) 
public void testCase1() { 
//line of code 
@Test(priority=1) 
public void testCase2() { 
//line of code 
}

Question 21 : How To Exclude A Particular Test Method From A Test Case Execution?

Answer :
Excluding a Particular Test Method From Test Case Execution 
<classes>
<class name="TestCaseName">
<methods>
<exclude name="TestMethodNameToExclude"/>//using exclude tag 
</methods>
</class>
</classes>

Question 22 : create a test having multiple groups

Answer :
Test having multiple groups 
@Test(groups={"group-one","group-two"}) //Two groups are defined here 
public void testMethodTwo(){ 
System.out.println("Test method two belonging to both 
group."); 
}

Question 23 : Write TestNG.xml code to run a group of test cases using group attribute of testng

Answer :
<suite name="Multi Group Suite" verbose="1">
<test name="Group Test one">
<groups>
<run>
<include name="group-one" />
</run>
</groups>
<classes>
<class name="test.groups.MultiGroup" />
</classed>
</test>
</suit>

Question 24 : List out various ways in which TestNG can be run?

Answer :
TestNG can be run with 
1.Eclipse IDE 
2.ant build tool 
3.command line 
4.IntelliJ’s IDEA

Question 25 : Explain how will you define dependencies in TestNG ?

Answer :
defining dependencies in TestNG 
1.dependsOnMethods in @Test annotations 
2.dependsOnGroups in @Test annotations

Question 26 : What are the Different build tools available?

Answer :
Different build tools available are 
1.Ant 
2.Maven 
3.Gradle

Question 27 : How to pass a parameter with a testng.xml file to use It In test case?

Answer :
  <parameter name="browser" value="FFX" /> //Parameter Tag you will write in TestNG.xml 
@Parameters ({"browser"}) //Parameters annotation you will define belwo @Test Annotation

Question 28 : Arrange testng.xml tags from parent to child.

Answer :
<suit> 
<test>
<classes>
<class>
<methods>

Question 29 : What are the two main ways to generate a report with TestNG?

Answer :
1.Listeners: For implementing a listener class, the class has to implement the org.testng.ITestListener interface. These classes are notified at runtime by TestNG when the test starts, finishes, fails, skips, or passes. 

2.Reporters: For implementing a reporting class, the class has to implement an org.testng.IReporter interface. These classes are called when the whole suite run ends. The object containing the information of the whole test run is passed to this class when called.

Question 30 : What is TestNG Reporting Support?

Answer :
TestNG generates test reports in HTML and XML formats. WebDriver does not have any native mechanism for generating reports.
TestNG window is more useful than console window in Eclipse as it generates text-based result while TestNG window generates a graphical output of the test result. 
It gives the Runtimes of each and every method and also the order in which methods are executed.

1 comment: