1. What Is TestNG?
Answer: TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing to integration testing.


2. What Are The Advantages Of Testng?
Answer: TestNG provides parallel execution of test methods.
It allows to define dependency of one test method over other method.
It allows to assign priority to test methods. @Test(priority=1)
It allows grouping of test methods into test groups.@Test(priority=1 groups=”Login”)
It has support for parameterizing test cases using @Parameters annotation.
It allows data driven testing using @DataProvider annotation.
It has different assertions that helps in checking the expected and actual results.
Detailed (HTML) reports.
TestNG Listeners.


3. What Are The Annotations Available In Testng?
@BeforeTest
@AfterTest
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
@BeforeSuite
@AfterSuite
@BeforeGroups
@AfterGroups
@Test
@DataProvider
@Parameters


4. How To Create And Run Testng.xml?
Answer: In TestNG framework, we need to create testng.xml file to create and handle multiple test classes. We do configure our test run, set test dependency, include or exclude any test, method, class or package and set priority etc in the xml file.


5. What Is The Importance Of Testng.xml File?
Answer: In a Selenium TestNG project, we use testng.xml file to configure the complete test suite in a single file. Some of the features are as follows:

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


6. How To Pass Parameter Through Testng.xml File To A Test Case?
Answer: We could define the parameters in the testng.xml file and then reference those parameters in the source files.

Create a java test class, say, ParameterizedTest.java and add a test method say parameterizedTest() to the test class. This method takes a string as input parameter. Add the annotation @Parameters(“browser”) to this method.

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”);
}}}
The parameter would be passed a value from testng.xml, which we will see in the next step.
We could set the parameter using the below syntax in the testng.xml file.
<parameter name=”browser” value=”firefox”/>
Here, name attribute represents the parameter name and value represents the value of that parameter.


7. What Is Testng Assert And List Out Common Testng Assertions?
Answer: TestNG Asserts help us to verify the condition of the test in the middle of the test run. Based on the TestNG Assertions, we will consider a successful test only if it is completed the test run without throwing any exception.

Some of the common assertions supported by TestNG are:

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)


8.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 the next step after the assert statement.

If there is any exception and you want to throw it then you need to use assertAll() method as a last statement in the @Test and test suite again continue with next @Test as it is.


9. What Is Hard Assert In Testng?
Answer: Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test.


10. What Is Exception Test In Testng?
Answer: TestNG gives an option for tracing the Exception handling of code. You can verify whether a code throws the expected exception or not. The expected exception to validate while running the test case is mentioned using the expectedExceptions attribute value along with @Test annotation.


Leave a Reply