TestNG Interview Questions
TestNG Interview Questions
com | 7263041604
@BeforeTest
@AfterTest
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
@BeforeSuite
@AfterSuite
@BeforeGroups
@AfterGroups
@Test
1|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
4. Can you arrange the below testng.xml tags from parent to child?
1 <test>
2 <suite>
3 <class>
4 <methods>
5 <classes>
1 <suite>
2 <test>
3 <classes>
4 <class>
5 <methods>
2|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
We could define the parameters in the testng.xml file and then reference
those parameters in the source files.
3 @Test
4 @Parameters("browser")
6 if(browser.equals("firefox")){
8 }else if(browser.equals("chrome")){
10 }
11 }
12 }
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.
Here, name attribute represents the parameter name and value represents
the value of that parameter.
3|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
Practical Example
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.
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.
Practical Example
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
4|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
Practical Example
We use priority attribute to the @Test annotations. In case priority is not set
then the test scripts execute in alphabetical order.
2 package TestNG;
3 import org.testng.annotations.*;
5 @Test(priority=0)
8 }
9 @Test(priority=1)
12 }
13 }
Output:
5|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
1 Test Case 1
2 Test Case 2
Parameterized tests allow developers to run the same test over and over
again using different values.
2 @DataProvider(name="getData")
10 data[1][0] = "SecondUid";
11 data[1][1] = "SecondPWD";
12
6|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
13 return data;
14
15 }
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.
3 System.out.println("Logged in successfully");
4}
Groups can also include other groups. These groups are called MetaGroups.
For example, you might want to define a group all that
includes smokeTest and functionalTest. Let’s modify our testng.xml file as
follows:
1 <groups>
2 <define name="all">
3 <include name="smokeTest"/>
7|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
4 <include name="functionalTest"/>
5 </define>
6 <run>
8 </run>
9 </groups>
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 <classes>
2 <class name="TestCaseName">
3 <methods>
4 <exclude name="TestMethodNameToExclude"/>
5 </methods>
6 </class>
8|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
7 </classes>
1 <groups>
2 <run>
3 <exclude name="TestGroupNameToExclude"/>
4 </run>
5 </groups>
To disable the test case we use the parameter enabled = false to the @Test
annotation.
1 @Test(enabled = false)
Once SkipException() thrown, remaining part of that test method will not be
executed and control will goes directly to next test method execution.
To ignore the test case we use the parameter enabled = false to the @Test
annotation.
1 @Test(enabled = false)
9|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
24. What are the different ways to produce reports for TestNG
results?
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. We
should implement this interface creating a listener class of our own. Next we
should add the listeners annotation (@Listeners) in the Class which was
created.
Practical Example
1 <methods>
2 <include name=".*smoke.*"/>
3 </methods>
27. What is the time unit we specify in test suites and test cases?
We specify the time unit in test suites and test cases is in milliseconds.
28. List out various ways in which TestNG can be invoked?
10 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
C:\Users\Admin\Desktop\STMSeleniumTutorial\workspace\SoftwareTestingMaterial
set
1
classpath=C:\Users\Admin\Desktop\STMSeleniumTutorial\workspace\SoftwareTestingMateri
2
al\bin;C:\Users\Admin\Desktop\STMSeleniumTutorial\workspace\SoftwareTestingMaterial\li
3
b\*
4
5
java org.testng.TestNG
C:\Users\Admin\Desktop\STMSeleniumTutorial\workspace\SoftwareTestingMaterial\testng.x
ml
The invocationcount attribute tells how many times TestNG should run a test
method
1 @Test(invocationCount = 10)
11 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
The threadPoolSize attribute tells to form a thread pool to run the test
method through multiple threads.
In this example, the method testCase1 will be invoked from three different
threads
In this example, the function testCase1 will be invoked ten times from three
different threads. Additionally, a time-out of ten seconds guarantees that
none of the threads will block on this thread forever.
@Factory: A factory will execute all the test methods present inside a test
class using a separate instance of the respective class with different set of
data.
12 | P a g e