TestNG Interview Questions Flashcards

1
Q

What is the TestNG framework?

What is the advantage we get using the TestNG framework?

Difference between TestNG and Junit?

What annotations are there in TestNG that are not in Junit?

A

TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing to integration testing.

The advantages of TestNG are as follows:
● TestNG is an open source framework that helps create test cases in a systematic way.
● TestNG gives lots of annotations which makes the test case creation easy.
● Using TestNG, priorities of the tests and the sequence of execution can be defined.
● Grouping is possible using TestNG.
● TestNG generates HTML reports (Selenium Webdriver cannot generate the test reports alone, TestNG helps SW to achieve this).
● Data parameterization is possible using TestNG.
● TestNG was built on top of Junit

TestNG vs Junit:
● TestNG supports testing in groups but it is not supported in JUnit.
● TestNG has a feature to configure dependency tests. Dependency test configuration for software web applications is not possible in JUnit.
● Test prioritization, Parallel testing is possible in TestNG. It is not supported by JUnit.
● TestNG compare to Junit provides additional annotations such as:
○ @BeforeSuite
○ @BeforeTest
○ @BeforeGroups
○ @AfterGroups
○ @Aftertest
○ @AfterSuite

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain TestNG annotations.

What annotations you have worked on?

What is the difference between before method and before test?

Describe all/few pre and post condition annotations.

A

BeforeMethod vs BeforeTest

● @BeforeMethod - The annotated method will be run after all the test methods in the current class have been run. @BeforeMethod will be executed just before
any function/method with @Test annotation starts.

● @BeforeTest - The annotated method will be run before any test specified in
.xml file using tag is run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to set test case priority in TestNG?

How To Make A Test Dependent On Another?

How to run a group of test cases using TestNG? How to disable a test case in TestNG ?

How to skip/exclude/block a particular Test method/Class from execution in TestNG?

A

Test Case Priority
We use priority attributes to the @Test annotations. In case priority is not set then the test scripts execute in alphabetical order.

Test Case Dependency
We can enforce TestNG’s dependency feature using “dependsOnMethods” attribute to declare dependencies of a test method.

Group Test Cases
TestNG allows you to perform groupings of test methods using “groups”

Disable a Test Case
To disable the test case we use the parameter enabled = false to the @Test annotation.

Skip/exclude Test Case
Using Exclude tag in our testng.xml

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a suite file? What is the importance of testng.xml file? How to create and run testng.xml?

A

In a Selenium TestNG project, we use testng.xml file to configure the complete test suite in a single file.

Importance of testng.xml file:
● Allows to include or exclude the execution of test methods and test groups
● Allows to add group dependencies
● Allows to add priorities to the test cases
● Allows to configure parallel execution of test cases

Create and run testng.xml file:
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the verification points available in TestNG?

Soft Assert vs Hard Assert in TestNG?

A

To perform validations in TestNG we are using assertions.

Assert: If the assert condition is true then the program control will execute the next test
step but if the condition is false, the execution will stop and further test steps will not be
executed.

Hard assertion uses Assert class. It breaks a test immediately with an AssertException after the assert method reports failure. If one of the many test cases fails, then the execution continues with the remaining tests.

Soft assertion uses SoftAssert Class. It doesn’t throw an exception on failure instead it collects all the errors until it finishes. And that’s how we could manage to run all the steps of a test @Test

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can we create a data driven framework using TestNG?

What will be the logic behind fetching data from a data provider and inserting it on UI?

A

The Data Driven framework is focused on separating the test scripts logic and the test data from each other and allows us to create test automation scripts by passing different sets of test data.

The test data set is kept in the external files or resources such as MS Excel Sheets, MS Access Tables, SQL Database, XML files etc.

Data-driven concept is achieved by @DataProvider annotation in TestNG.

The @Test method that wants to receive data from this DataProvider needs to use a dataProvider
name equals the name of this annotation.

If you want to provide the test data, the DataProvider way, then we need to declare a method that returns the data set in the form of two dimensional object array Object[][].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a TestNG Listener?

How did you use it?

How to get a screenshot on failure?

A

TestNG listeners mainly used to generate the report for the test.

You can capture a screenshot when there is a test failure.

TestNG events are like on Test Failure, onTestSkipped, onTestSuccess, etc.

Using ITestListener interface and method onTestFailure we can get a screenshot of our failed test case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the different ways to produce reports for TestNG results?

A

TestNG library brings a very convenient reporting feature. Once we execute our tests, TestNG generates a test output folder at the root of the project.It combines two kinds of reports.

Detailed Report - You can find this report in the file. It combines the detailed information like the errors, test groups, execution time, step-by-step logs and TestNG XML file.

Summary Report - It is the trimmed version and informs about the no. of “Passed”/”Failed”/”Skipped” cases. You can see it from the file. It’s an email friendly report which you can embed and share with the stakeholders.

Listener - defined as an interface that modifies the default TestNG behavior. As the name suggests Listeners “listen” to the event defined in the selenium script and behave accordingly. It is used in selenium by implementing Listeners Interface. It allows customizing TestNG reports or logs.

ExtendReports - Extent Report is an HTML reporting library for Selenium WebDriver for Java. It is a great tool we can use within our automation framework to make excellent execution reports.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you run multiple TestNG Suite Files?

A

We have to configure our testng.xml file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to run test cases in parallel using TestNG?

A

Use “parallel” attribute in testng.xml to accomplish parallel test execution in TestNG

tests – All the test cases inside 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly