TestNG Questions and Answers for Selenium Interview
What is TestNG?
- TestNG is a Java testing framework, inspired by JUnit and NUnit.
- NG stands for Next Generation.
- TestNG is designed to cover unit, functional, end-to-end, integration, etc.,
What are the advantages of TestNG?
- 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.
- It allows grouping of test methods into test groups.
- 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.
What are the annotations available in TestNG?
- @BeforeTest
- @AfterTest
- @BeforeClass
- @AfterClass
- @BeforeMethod
- @AfterMethod
- @BeforeSuite
- @AfterSuite
- @BeforeGroups
- @AfterGroups
- @Test
What is the importance of testng.xml file?
Using testng.xml file configure the complete test suite in a single file.
- The 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.
How to pass parameter through testng.xml file to a test case?
- We 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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.
1 | <parameter name="browser" value="firefox"/> |
What is TestNG Assert and list out common TestNG Assertions?
- 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.
- 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)
What is Soft Assert in TestNG?
- Soft Assert collects errors during @Test.
- Soft Assert Execute the remaining test case even after an assertion fails.
What is Hard Assert in TestNG?
- Hard Assert throws AssertException immediately, test is marked as failed and test suite continues with next @Test.
What is exception test in TestNG?
- You can test whether a code throws a desired exception or not.
- Here the expectedExceptions parameter is used along with the @Test annotation.
E.g:
1 | @Test(expectedExceptions = ArithmeticException.class) |
How to set test case priority in TestNG?
- We use priority attribute to the @Test annotations.
- In case priority is not set then the test scripts execute in alphabetical order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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"); } } |
Output:
1 2 3 | Test Case 1 Test Case 2 |
What is parameterized testing in TestNG?
- Using Parameterized testing allow developers to run the same test over and over again using different values.
How can we create data driven framework using TestNG?
- By using @DataProvider annotation we can create a Data Driven Framework.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package TestNG; import org.testng.annotations.*; public class PriorityTestCase{ @DataProvider(name="getData") public Object[][] getData(){ //Object [][] data = new Object [rowCount][colCount]; Object [][] data = new Object [2][2]; data [0][0] = "Firstuser"; data [0][1] = "Firstpass"; data[1][0] = "Seconduser"; data[1][1] = "Secondpass"; return data; } } |
How to run a group of test cases using TestNG?
- One test able to assigned multiple groups using “groups” in TestNG.
- Groups are specified in your testng.xml file and can be found either under the <test> or <suite> tag.
- Groups specified in the <suite> tag apply to all the <test> tags underneath.
1 2 3 4 5 6 7 | @Test (groups = { "smokeTest", "functionalTest" }) public void loginTest(){ System.out.println("Logged in successfully"); } |
How to create Group of Groups in TestNG?
- Groups can also include other groups. These groups are called MetaGroups.
Add groups into TestNG.xml file to Execute.
1 2 3 4 5 6 7 8 9 10 11 | <groups> <define name="all"> <include name="smokeTest"/> <include name="functionalTest"/> </define> </groups> |
How to run test cases in parallel using TestNG?
Using “parallel” attribute in testng.xml to accomplish parallel test execution in TestNG.
The parallel attribute of suite tag can accept four values:
- tests– All the test cases inside <test> tag of testng.xml file will run parallel.
- classes– All the test cases inside a java class will run parallel.
- methods– All the methods with @Test annotation will execute parallel.
- instances– Test cases in same instance will execute parallel but two methods of two different instances will run in different thread.
1 | <suite name="parallelSTQATools" parallel="methods"> |
How to exclude a particular test method from a test case execution?
- By adding the exclude tag in the testng.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <classes> <class name="class_name"> <methods> <exclude name="MethodName_ToExclude"/> </methods> </class> </classes> |
How to exclude a particular test group from a test case execution?
- By adding the exclude tag in the testng.xml we Skip / Exclude test from TestNG.xml file.
1 2 3 4 5 6 7 | <groups> <run> <exclude name="TestGroupNameToExclude"/> </run> </groups> |
How to disable / ignore a test case in TestNG?
- Disable / Ignore the test case we use the parameter “enabled = false” to the @Test annotation.
1 | @Test(enabled = false) |
Different ways to produce reports for TestNG results?
- Listeners implement the interface org.testng.ITestListener and are notified in real time of when a test starts, passes, fails, etc…
- Reporters implement the interface org.testng.IReporter and are notified when all the suites have been run by TestNG. The IReporter instance receives a list of objects that describe the entire test run.
What is the use of @Listener annotation in TestNG?
- TestNG listeners are used to configure reports and logging.
- One of the most widely used listeners in testNG is ITestListener interface.
- It has methods like onTestStart, onTestSuccess, onTestFailure, onTestSkipped etc.
What is the use of @Test(invocationCount=x)?
- The invocationcount attribute tells how many times TestNG should run a test method.
1 2 3 4 5 | @Test(invocationCount = 10) public void method_name(){ } |
- The method method_name() will be invoked ten times.
Explain what does @Test(invocationCount=?) and (threadPoolSize=?) indicates?
- @Test (threadPoolSize=?): The threadPoolSize attributes tell TestNG to form a thread pool to run the test method through multiple threads. With threadpool, the running time of the test method reduces greatly.
- @Test(invocationCount=?): The invocationcount tells how many times TestNG should run this test method.
How to skip a @Test method from execution?
- Using SkipException inside @Test method to skip a test case from test execution.
1 | throw new SkipException("Test Check_Checkbox Is Skipped"); |
- It will throw skip exception and @Test method will be ignored immediately from execution.
Why soft assertion is used in Selenium WebDriver and TestNG automation project?
- Using TestNG soft assertion continue the test execution even if the assertion is failed.
- That means once the soft assertion fails, remaining part of <@Test> method is executed and the assertion failure is reported at the end of <@Test> method.
Basic steps for drafting TestNG test cases?
- Write down the business logic of your test.
- Add appropriate TestNG annotations in your code.
- In <build.xml> or <testing.xml>, add the information about your test.
- Run your TestNG project.
What are the different attributes for @Test annotation?
- alwaysRun, dataProvider, description, enabled, expectedExceptions, groups, priority, timeOut
1 | E.g: @Test(timeOut = 2000) |
TestNG Questions and Answers for Selenium Interview and TestNG Questions and Answers for Selenium Interview and TestNG Questions and Answers for Selenium Interview and TestNG Questions and Answers for Selenium Interview