Search Tutorials


TestNG (2024) Interview Questions | JavaInUse

TestNG Interview Questions


In this post we will look at TestNG Interview questions. Examples are provided with explanation.


  1. What is TestNG?
  2. What are the features of TestNG?
  3. What are Basic Annotations in TestNG?
  4. What are the different Assertions in TestNG?
  5. What is Data Provider in TestNG?
  6. What is Parameterized Testing?
  7. What are the ways by which you can produce reports for TestNG results?
  8. How can we set the priorities in TestNG?
  9. How do we run multiple test suites in TestNG?
  10. What is InvocationCount in TestNG?
  11. How do we disable entire unit test in TestNG?
  12. How can we run TestNG from Command Line?
  13. What is TestNG Assert?
  14. What are the types of Asserts in TestNG?

What is TestNG?

TestNG is a developed framework from scratch level and helps in providing in full cover the test cases and in executing the test.It is known as Testing Framework.It is used in Java Programming Language that is developed by Cedric Beust, and is also used for the inspiration from the JUnit Framework.TestNG helps in adding more powerful functionality and is easy to use.

What are the features of TestNG?


TestNG

  • TestNG supports Annotations.
  • TestNG helps in using Java and OO Features.
  • TestNG supports TIC(Testing Integrated Classes).
  • TestNG is flexible plug in API.
  • TestNG supports Multi Threaded Testing.
  • TestNG is used in separating compile time test code from run time data information.

What are Basic Annotations in TestNG?


TestNG

  • BeforeSuite - used in running a single time before any tests.
  • BeforeTest - used in executing the test methods in class.
  • BeforeClass - used in executing the first tests in an invoked class.
  • BeforeMethod
  • Test - used in identifying the method of execution.
  • AfterMethod
  • AfterClass - used in executing tests in invoked classes.
  • AfterTest - used in running tests methods.
  • AfterSuite - used in running all the other tests.

What are the different Assertions in TestNG?

Different types of Assertions are as following:
assertEquals used in passing arguments that are equal otherwise fails.
assertNotEquals used in expecting values matches when it is failed.
assertTrue helps in passing the input condition which are true otherwise fails.
assertFalse helps in passing the input condition which is false otherwise fails.

What is Data Provider in TestNG?

Data Provider is used in providing test cases for execution and in helping with data driven test cases which carries the methods and can also be runned multiple times with different data sets.Data Provider also helps in giving complex parameters in the test methods.




What is Parameterized Testing?

Parameterized is used in executing the same test again and again using different values.It also allows in passing parameters directly to our test methods: By using testing.xml.
By using Data Providers.

What are the ways by which you can produce reports for TestNG results?

There are 2 ways by which we can produce reports:
Listeners helps in implementing org.testing interface and also informing classes at runtime by TestNG.
Reporters used in consisting the information of the test run and delivering it to the class.

How can we set the priorities in TestNG?

We can set the priorities by using the following command:
package org;  
import org.Testng.Annotations.Test;  
public class Testmethods  
{  
@Test(priority=3)  
public void test1()  
{  
System.out.println("Test1");  
}  
@Test(priority=1)  
public void test2()  
{  
System.out.print("Test3");  
}  
}  


How do we run multiple test suites in TestNG?

We can run multiple test suites by using the following command:
<suite name="All Suites">
  <suite-files>
    <suite-file path="suite2.xml" />
    <suite-file path="suite3.xml" />
  </suite-files>
  </suite>


What is InvocationCount in TestNG?

We can execute all the test case any number of times and then we can use InvocationCount Attribute given in the following example:
@Test(InvocationCount=5)
Public void print()
{
}


How do we disable entire unit test in TestNG?

We can disable the entire unit test by using the following command:
@Test(enabled=true)
public class footest {
  //
}


How can we run TestNG from Command Line?

We can use the following testng.xml file for the following:
<!DOCTYPE suite SYSTEM "testng.org" >  
<suite name="suiteall" verbose="1">
    <test name="testall">  
        <packages>  
            <package name="com.project.fred.tests"/>
        </packages>  
    </test>  
</suite>


What is TestNG Assert?

TestNG Assert helps in verifying conditions of the test in the middle of the test run.
There are some common assertions supported by TestNG:
assertEqual(String actual,String expected)
assertEqual(String message)
assertEquals(boolean actual,boolean expected)
assertTrue(condition)
assertTrue(message)
assertFalse(condition)
assertFalse(message)


What are the types of Asserts in TestNG?

There are 2 types of Asserts in TestNG:

TestNG

Hard Assert - used for validating TestNG Class:
Soft Assert - used in continuing test execution after the assert statement fails.


See Also

Spring Boot Interview Questions Apache Camel Interview Questions Drools Interview Questions Java 8 Interview Questions Enterprise Service Bus- ESB Interview Questions. JBoss Fuse Interview Questions Top ElasticSearch frequently asked interview questions