Site icon TestingDocs.com

TestNG Automation Framework Questions

In this post, lets look into some of the TestNG automation framework questions and answers to them.

What is TestNG framework ?

TestNG is inspired by JUnit and NUnit frameworks. It is an open-source framework for functional test automation. It provides an API for adding custom implementations as well. Also, TestNG has a better reporting structure and it provides XML and HTML default reports.TestNG is primarily used for functional testing, unlike JUnit which is mostly used for unit testing. In addition, we can drive testng different configuration runs using the XML file. Furthermore, TestNG methods are specified with @Test annotation in the code.

What are TestNG framework features?

Main TestNG features are:

Open  TestNG API .
TestNG provides easy extension of API, this helps in adding custom extensions or plugins to the framework depending upon the requirement.

What is method dependency in TestNG?

Sample snippet below that depicts method dependency in TestNG framework.

@Test() 
 public void method1(){ 
 System.out.println("method1"); 
 } 
  
   
 @Test(dependsOnMethods={"method1"}) 
 public void dependent(){ 
 System.out.println("This method depends on method1, this method will  
skip if method1 fails"); 
 }

 

What is @Factory annotation in TestNG?

@Factory in TestNG framework creates tests dynamically at run-time. A factory method is defined by declaring @Factory above the respective test method. It is mandatory that a factory method should return an array of Object  i.e Object[]
TestNG factory is used to create instances of test classes. This is useful if you want to run the test class any no of times.

Example code snippet:

 
public class SampleFactory
 {
  @Factory
  public Object[] factoryMethod()
 {
  Object[] object = new Object[5];
  for (int i = 0; i < 5 ; i++)
     {
     object[i] = new SampleTest();
       }
  return object;
  }
 }

 

Exit mobile version