Top 50+ TestNG Interview Questions and Answers For 2021 - Scientech Easy

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

Home Core Java String Collections Selenium Interview Thread Exception Handling Blog

Top 50+ TestNG Interview Questions and Answers


Ads by for 2021 Selenium Tutorials
We'll tryAdnotclosedtoshowby that ad again
Stop seeing this ad
Why this ad? TestN G Interview Q uestions |TestN G , Ads by that ad again A utom on Testing +

We'll tryAdnotclosedtoshowby
w here N G stands for “next generation” Stop seeing this ad Selenium W ebD river +

is the next step tow ards w riting test Why this ad?
Selenium W ebD river Com m ands +
cases.
Selenium Loc rs +
It w as developed by C edric B eust to
Selenium XP Tutorials +
overcom e the de ciency in JU nit by
introducing som e m ore functionalities isD isplayed,isEn ed,isSelected
and features. M ethods +

R o Button and Checkbox +


H ere,w e have listed topic w ise the best
collection of frequently asked TestN G interview questions w ith the best Selenium Select Class +
possible answ er for the interview .
A lert in Selenium +

The list of top 50+ TestN G interview questions and answ ers are for freshers
A ction Interface and A ctions Class +
and experienced.
Selenium JavascriptExecutor +
Topic-wise TestNG Interview Questions with
Answers for Fresher IFram e in Selenium +

TestNG Tutorials +

A he PO IA PI Tutorial +

PO M in Selenium +

Selenium Fram ew ork Tutorial +

Selenium Interview Q uestions +

Frequently Asked Interview Questions on TestNG

1.W hat is TestN G ?

TestN G is an open-source autom ation testing fram ew ork based on JU


nit by introducing som e m ore functionalities and features.So,TestN G is m
ore pow erfulthan JU nit fram ew ork.

It can be integrated w ith Selenium or any other autom ation toolto provide m
ultiple features like assertions,reporting,paralleltest execution,etc.

2.W hat are the im portant features of TestN G ?


TestN G include the follow ing features that are as follow s:
1.TestN G uses m ore object-oriented and java features. Ads by We'll
tryAdnotclosedtoshowby that ad again
2.TestN G supports m ultiple types of B efore/A fter annotations to create test Stop seeing this ad
cases.
Why this ad?
3.It provides different types of assertions that helps in checking actualand
expected results.
4.It provides priority and grouping features by w hich test cases can be
grouped and prioritized easily.
Ads by
We'll tryAdnotclosedtoshowby that ad again
Stop seeing this ad

Why this ad?

5.TestN G internally generates H TM L test report in a readable form at.


6.It supports paralleltesting,integrated testing,load testing,etc.
7.TestN G allow s D ata-driven testing using @ D ataProvider annotations.
8.It supports for param eterizing test cases using @ Param eters annotation.

3.W hat are som e advantages of TestN G over JU nit?

TestN G have the follow ing advantages over JU nit.They are as follow s:
1.TestN G generates H TM L test report in a readable form at.
2.Test m ethods can be grouped and prioritized m ore easily that is not
possible in the case of JU nit.
3.A nnotations of TestN G are easy to understand for creating test cases.

4.In TestN G ,w e can execute m ultiple test cases in parallelto reduce


execution tim e.
5.U sing TestN G ,w e can de ne the dependency of one test m ethod over
another.
6.TestN G doesn’t need to extend any class.
7.TestN G supports three additionalannotations @ B efore/A fter Suite,
@ B efore/A fter Test,and @ B efore/A fter G roup.

4.W hy do w e need TestN G in Selenium ?

There are follow ing reasons for integrating TestN G w ith Selenium .
1.Since Selenium W ebD river does not generate test report,by integrating
TestN g w ith Selenium ,w e can easily generate test reports/results in a proper
readable form at to know about how m any test cases are passed,failed,and
skipped.

2.W e can execute selective test cases in TestN G .


3.U sing TestN G ,w e can also execute failed test cases separately.
4.TestN G fram ew ork can be integrated easily w ith other tools like M aven,
Jenkins,etc.
5.W e can easily organize test cases in a proper w ay.
Ads by
We'll tryAdnotclosedtoshowby that ad again
Stop seeing this ad Why this ad?

Live SD-WAN Demo


Scale, speed and reach, automated for warp speed deployments.

5.W hat are the basic steps required in w riting TestN G test?

There are three basic steps required in w riting TestN G test that is as
follow s:
1.W rite dow n the business logic of the test and annotate it by TestN G
annotations.
2.C reate testing.xm l le and add the inform ation about your test
3.Ru n TestN G .

6.H ow to run test script in TestN G ?

We can run test script in TestN G by right-clicking on the TestN G class,click


on “Ru n A s” option and then select “TestN G test”.

For m ore details go to link:https://w w w .scientecheasy.com /2020/07/how -to-


w rite- rst-testng-test-case-in-eclipse.htm l/

Frequently Asked Questions on TestNG Annotations

1.W hat are A nnotations in TestN G ?

A nnotations in TestN G are a set of code that controls how m ethod below
them have to be executed i.e,the order of execution of m ethods below them
w illbe decided by annotations that w e give.

2.W hat are the different annotations available in the TestN G ?

TestN G supports a pow erfulset of different annotations that run tests in


an organized and predictable m anner.

a.Precondition annotations

Precondition annotations are those annotations of TestN G that are executed


before the execution of test m ethods The Precondition annotations are @ B
eforeSuite,@ B eforeC lass,@ B eforeTest,@ B eforeM ethod.

b.Test annotation

M ethods annotated w ith @ Test are called test m ethods w hich serve as a unit
test.

c.Postcondition annotations

The postcondition annotations are those annotations that are executed after the
execution of allthe test m ethods.The postcondition annotation can be @ A
fterSuite,@ A fterC lass,@ A fterTest,@ A fterM ethod.

3.W hat is the sequence of execution of allTestN G annotations?

The sequence of execution of allannotations in TestN G is as follow s:

@ B eforeSuite
@ B eforeTest
@ B eforeC lass
@ B eforeM etho
@Test

@ A fterM ethod
@ A fterC lass
@ A fterTest
@ A fterSuite

4.W hat is difference betw een Suite,Test,and Class?

Suite:A suite is m ade of one or m ore tests.


Test:A test is m ade of one or m ore classes.
C lass:A class is m ade of one or m ore m ethods.

5.W hat is the difference betw een @B eforeClass and @B eforeM ethod?

There are tw o m ain differences betw een @ B eforeC lass and


@ B eforeM ethod.They are as follow s:

1.The m ethod w ith @ B eforeC lass w illbe executed only once before any of
the tests in the current class are run w hereas,a m ethod annotated w ith
@ B eforeM ethod w illbe executed before each m ethod annotated w ith @ Test.

2.@ B eforeC lass annotation can be used to set up the con guration and
initialization w hich is com m on to alltest m ethods in the current class.For
exam ple,w e can set up driver con guration w hich w illbe com m on for all
tests in the class.

@ B eforeM ethod can be used to set that data w hich is repeating before each
@ Test annotated m ethod.

6.W hat is the test m ethod?

A m ethod annotated w ith @ Test is called test m ethod w hich serves as a


unit test.In the @ Test m ethod,w e w illw rite the logic of the application w hich
w e w ant to autom ate.

7.W hat is the retu type of @ D ataProvider annotation provided by


TestN G ?

@ D ataProvider annotation return an object double array (O bject [ ][ ]) as data to the test m

ethod.

8.W hat is the retu type of @ Factory annotation?

This annotation returns an array of class objects (O bject [ ]).

For m ore details go to this tutorial:TestN G A nnotations

Frequently Asked Interview Questions on TestNG Priority and


Groups

1.W hat is Priority in TestN G ?

Priority is an attribute that tells TestN G w hich orders have to follow to run
test m ethod.It is used to set the order of test cases as ours need.

2.W hat is the default value for TestN G Priority?

The default value for TestN G priority is zero.

3.H ow to set Priority of test cases in TestN G ?


TestN G priority attribute is usefulto execute the test cases in order.It has
the follow ing generalform to set the priority of test cases.
Syntax:

@ Test(prioirty = 1) // Valid syntax.

@ Test(PRIO RITY = 1) // Invalid sysntax.ID E w illshow it as a com pilation error.

4.H ow w illyou execute m ethods or test cases in TestN G in different


order/your order?

When w e have m ultiple m ethods or test cases and w ant to execute in


order,TestN G priority attribute is usefulto execute m ultiple test cases in your
order.If you have not m entioned test priority,TestN G w illassign all@ Test a
priority as zero(0) and execute them .

5.H ow w illyou controlm ultiple test cases?

Re fer to A nsw er 4.

6.D e ne TestN G G roups.

G roups is an attribute in TestN G that allow s us to perform groupings of test


m ethods or test cases.It allow s us to declare a set of test m ethods in a
particular nam ed group or m ultiple groups and run m ultiple groups of tests at
different tim es or in different places.

For exam ple,if w e have 100 test cases of U niversity and 10 test cases of C
ollege,and if you w ant to run allthe test cases of U niversity together in a
single suite,this can be possible only through the grouping of test cases.

7.H ow to group m ultiple test m ethods in a single group using TestN G ?

We can group m ultiple test m ethods by using groups param eter in the
@Test annotation.The syntax for grouping m ultiple test m ethods in a single group is as follow

s:

Syntax:

@ Test(groups = {“G roupN am e”})

For exam ple:

@ Test(groups = {“C hem istry”}) public

void atom () {

…………
}

@ Test(groups = {“C hem istry”}) public

void electorn() {

…………….
}

B oth test m ethods w illexecute in one group nam ed C hem istry.

8.H ow to group m ultiple test m ethods in m ultiple groups?

The grouping of test m ethods belonging to m ultiple groups can be done by


providing the group nam es as an array in the groups attribute of the @ Test
annotation.The below syntax lets you add test m ethods in m ultiple groups.
Syntax:

@ Test(groups = { “G roupN am e1”,“G roupN am e2”,“G roupN am e3” … .})

9.H ow to group m ultiple test m ethods w ith Priority?


You can also group m ultiple test m ethods w ith priority.The syntax for
grouping test m ethods w ith priority is as follow s:
Syntax:

@ Test(groups = {“G roupN am e”},priority=0) // The test m ethod


annotated w ith this group w illexecute rst.
@ Test(groups = {“G roupN am e”},priority=1) // The test m ethod
annotated w ith this group w illexecute after executing the rst group.

10.W hat is Inclusion & Exclusion G roups in TestN G ?

A group that is included in test execution is called inclusion group.A group w


hich is excluded from test execution is called exclusion group.

11.H ow to disable a test in TestN G ?

We can also disable tests on an individualbasis by using the “enabled”


property available on both @ Test and @ B efore/A fter annotations.The syntax
can be like this:
Syntax:

@ Test(enabled = false)

@ Test(groups = {“C ricket Player”},enabled=false)

12.W hat is D efault G roup,PartialG roups,and M etaG roups in TestN G ?

D efault G roup:W hen an entire class is added to a group,it is called default


group.It is a good w ay of de ning a default group for allunit tests w ithin a class.

PartialG roups:W hen you de ne groups at the class leveland then add groups
at the m ethod level,it is called partialgroups.

M etaG roups:W hen groups include other groups,these groups are called m
etagroups.

For m ore detail,go to tutorial:TestN G G roups

Frequently Asked Interview Questions on TestNG @Test


Annotation & its Supported Attributes

1.W hat is @ Test A nnotation in TestN G ?

@Test annotation is a basic annotation in TestN G that m arks a m ethod as a test m

ethod.The test m ethod annotated w ith @ Test annotation tells TestN G that this m ethod is a

“test” and it should be executed w hen user runs the class.

2.W hat are the attributes supported by @ Test annotation in TestN G ?

@Test annotation supports lots of attributes that w e can use w ith this annotation.Som e of

the im portant attributes supported by Test annotation are alw aysRu

n,dataProvider,dataProviderC lass,dependsO nG roups, dependsO nM

ethods,enabled,priority,tim eO ut,etc.

3.W hich attribute is used to run test m ethod alw ays?

The attribute “alw aysRu n” supported by @ Test annotation is used to run


test m ethods alw ays.It takes value as true or false.If w e set true,this m ethod
w illalw ays execute even its depending m ethod fails.It has the follow ing
generalform .
Syntax:
@ Test(alw aysRu n = true)

4.W hich attribute is used to provide data to test m ethod in D ata-driven


testing?

The dataProvider attribute is used to provide data to the test m ethod


directly in data-driven testing.
Syntax:

@ Test(dataProvider = “getD ata”)

5.W hat is dependency in TestN G ?

When w e w ant to execute m ultiple test cases in a speci c order,then w e


use the concept of dependency in TestN G .There are tw o types of dependency
attributes used in TestN G .They are:

a.dependsO nM ethods:The attribute dependsO nM ethods is used to m ake the


test m ethod depend on a particular m ethod.The test m ethod annotated w ith
@Test and attribute dependsO nM ethods w illrun after executing allthose m ethods on w

hich this test m ethod is dependent.

Syntax:

@ Test(dependsO nM ethods={“M ethod1”,“M ethod2”...})

b.dependsO nG roups:This attribute is used to m ake test m ethods depend on a


particular group.A llof the m ethods of these groups are executed rst before this
m ethod.
Syntax:

@ Test(dependsO nG roups={“G roupA ”,“G roupB ”,...})

6.H ow w illyou m ake test cases dependent on each other?

Re fer answ er no.5.

7.H ow w illyou skip a particular test case in TestN G ?

W e can skip or disable a particular test case using enabled attribute.It can be
achieved by setting enabled attribute of the Test annotation to false. Syntax:

@ Test(enabled = false)

8.W hat is invocationCount in TestN G ?

invocationC ount in TestN G is an attribute that is used to execute a m ethod


or test in the num ber of tim es.It acts as a loop.
For exam ple:

@ Test(invocationC ount = 5) H ence,this m ethod w illexecute 5 tim es.

9.W hat is tim eO ut in TestN G ?

tim eO ut in TestN G is an attribute that is used for a tim e out test.It


speci es a tim e period (in m illiseconds) to w ait for a test for com plete
execution.

For m ore detail,go to tutorial: TestN G @ Test A nnotation & Supported


A ttributes

FAQs on TestNG XML File

1.W hat is testng.xm l le?


Testng.xm l le is a con guration le (XM L le) for TestN G in w hich w e can
create test suites,test groups,m ark tests for parallelexecution,add listeners, and
pass param eters to test script.It de nes the runtim e de nition of a test suit.

2.W hat is the im portance or use of testng.xm l le?

There are follow ing im portance or usage of the testng.xm l le in TestN G .


They are as follow s:
1.The testng.xm l le can be used to controlthe execution of w hole tests from a
single le.
2.W e can run a set of test cases from a single place.
3.W e can pass param eters to test m ethods or classes.
4.U sing testng.xm l le,w e can perform paralleltest execution.
5.W e can add the listener.

6.It de nes the order of the execution of allthe test cases.


7.It allow s us to group test cases and can be executed as per requirem ents.
8.It helps to execute selected test cases.
9.W ith the help of testng.xm l le,w e can integrate TestN G fram ew ork w ith
Jenkins.

3.H ow to pass param eters in test cases through testng.xm l le?

We can pass the param eter value to test m ethods at runtim e through the
testng.xm l le.For this,w e can use @ Param eter annotation:
Syntax:

@ Param eter(“param -nam e”);

4.W hat is thread-count in TestN G ?

The thread-count in TestN G is an attribute that is used to run the


m axim um num ber of threads for each suite if the parallelm ode is enabled (otherw ise
ignore it).

For exam ple,thread-count = “2”:It w illrun your tests w ith tw o threads.

5.W hat is verbose in TestN G ?

verbose is an attribute in TestN G w hich is m ostly used w hen reporting a


bug or w hen trying to diagnose the execution of test run.

6.H ow to create and run testng.xm l le in Eclipse?

Re fer to below tutorial.

7.H ow to enable or disable test cases using testng.xm l le?

We can enable or disable test cases by using <include> and <exclude>


tags in the testng.xm l le.

For m ore detail,click on this tutorial:H ow to create and run TestN G XM L le in


Eclipse?

FA Interview Questions on TestNG Parameterization and


Parallel Testing

1.W hat is Param eterization in TestN G ?

Param eterization is a feature provided by TestN G w hich allow s the users


to pass param eter values to test m ethods as argum ents.
U sing this feature,w e can run the sam e test over and over again w ith
different values.The param eterization test is supported by using Param eter
and D ataProvider annotations of TestN G .

2.H ow m any w ays by w hich w e can pass param eter values to test
m ethods?

There are m ainly tw o w ays through w hich w e can directly pass param eter
values to test m ethods.
1.Through testng.xm l le.
2.Through D ataProviders

3.W hich annotation is used to pass param eter values to test m ethod from the
testng.xm l le?

Param eter annotation.

4.W hat is D ataProvider?

D ataProvider feature provided by TestN G is the second w ay of passing


param eters to test m ethods.It allow s the users to w rite data-driven tests in w
hich w e can run m ultiple tim es the sam e test m ethod w ith different sets of
test data.

TestN G provides a @ D ataProvider annotation to use the D ataProvider feature


in tests.This annotation is declared w ith a test m ethod in the class.

@ D ataProvider annotation takes an attribute “nam e” called dataProvider in the Test

annotation.It has the follow ing generalsyntax/form to use. Syntax:

@ D ataProvider(nam e = “m yTestD ata”)

5.W hat is the retu type of D ataProvider?

D ataProvider returns a tw o dim ensionalan array of object (O bject [ ][ ]) to


the test m ethod.

6.H ow to CallD ataProvider from another Class?

In the test m ethod,w e w illadd one m ore attribute “dataProviderC lass” in


@Test annotation.The syntax is like this: Syntax:

@ Test(dataProvider = “getD ata”,dataProviderC lass = D

ataProviderC lass.class)

H ere,D ataProviderC lass is the nam e of class.

7.W hat are the advantages of TestN G D ataProvider feature?

The advantages of data provider in TestN G are as follow s:


1.TestN G D ata provider helps to pass param eter values directly to the test
m ethod.

2.It allow s users to w rite data-driven test w here w e can run m ultiple tim es
the sam e test m ethod w ith different sets of test data.

8.W hat is ParallelTesting in TestN G ?

ParallelTesting is a testing technique in w hich m ultiple tests are executed


sim ultaneously in different threads to reduce execution tim e.

9.H ow to run ParallelTests,Classes,& M ethods in Selenium using TestN G


XM L File?
In Selenium ,w e can run our test m ethods/classes/tests in parallelby using
the “parallel” attribute for Test Suite in the testng.xm l le.

For m ore detail,click on the tutorial:ParallelTest Execution in Selenium

FAQs on TestNG Assertion

1.W hat is A ssertion in TestN G ?

A n assertion is basically blocks of code that are placed in the test cases to
check or verify w hether the test has failed or passed.A test w illbe considered
as successfulO N LY if test is com pleted w ithout throw ing any exception.i.e,If
the conditions are not true (do not satisfy),it w illstop the further execution of the
test and m arks it as failing.

2.W hy do w e use A ssertion in Selenium ?

A n assertion in selenium is used to verify the result or TestN G reports.It is


m ainly used to com pare the actualresult of an application w ith the expected result.

3.W hat are the types of assertions in TestN G ?

There are tw o types of assertions present in the TestN G fram ew ork.They


are as follow s:
1.H ard A ssertion
2.Soft A ssertion

H ard A ssertion in TestN G

H ard assertion is a type of assertion that throw s an A ssertException w hen


the assertion condition does not satisfy and the test case is im m ediately m
arked as failed.

A n exam ple of hard assertion m ethods are assertEquals,assertN otEquals,


assertTrue,assertFalse,assertN ull,assertN otN ull.

Soft A ssertion in TestN G

Soft assertion is a type of assertion that continues w ith test execution even if
the assertion condition is not m et.That m eans soft assertions do not throw an
exception w hen the assert statem ent (assertion condition) fails.

Recom m ended Posts for Interview

Topic-w ise Selenium Interview Q uestions and A nsw ers

Top 30+ Selenium Fram ew ork Interview Q uestions and A nsw ers

Java Interview Q uestions for Selenium A utom ation Testing

H ope that this tutorialhas covered TestN G interview questions w ith the best
possible answ ers.In the future,if need,questions w illbe updated regularly
related to TestN G .A llTestN G interview questions are frequently asked for
freshers as w ellas 2 to 3 years of experience. Thanks for reading!!!

Interview ,TestN G
TestN G Interview Qu estions

Leave a Comment

You might also like