Testing Tutorial

TestNG Tutorial – Complete Guide For Testers | Software Testing Material

TestNG Tutorial – Complete Guide

In this TestNG Tutorial, you will learn how to work with the TestNG and implement it while working with Selenium. Check this to learn Selenium Tutorial for Free

What is TestNG?
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing to integration testing. TestNG is a testing framework inspired from JUnit and NUnit. It has more functionalities compared to JUnit and NUnit and it makes more powerful and easier to use.

Who is the targeted audience of this TestNG Tutorial?
This tutorial is designed for Software Testers, QA Engineers interested in learning the features of TestNG Framework in simple and easy steps.

TestNG Video Tutorial:
Few people think that video learning is better than reading a blog post. If you are one here is the video tutorial.

 

TestNG Introduction | TestNG Tutorial:

TestNG Introduction:

Definition of TestNG as per its documentation is as follows:

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

Writing a test is typically a three-step process:

  • Write the business logic of your test and insert TestNG annotations in your code.
  • Add the information about your test (e.g. the class name, the groups you wish to run, etc…) in a testng.xml file or in build.xml.
  • Run TestNG.xml.

Later in this tutorial, we will discuss about TestNG Annotations, testng.xml.

TestNG is a framework for Java, so the very first requirement is to have JDK installed in your machine.

Verify Java Installation in your system

Output:

Set Java Environment:

Set the JAVA_HOME environment variable to point to the base directory location, where Java is installed on your machine. For example,

Set the environment variable JAVA_HOME to C:\Program Files\Java\jdk1.8.0_101

Append the string C:\Program Files\Java\jdk1.8.0_101\bin at the end of the system variable, ‘Path

 

TestNG Annotations And Benefits | TestNG Tutorial:

TestNG Annotations:

In this post, we see the list of TestNG Annotations. You could learn complete TestNG Tutorial here. Here is a quick overview of the annotations available in TestNG.

@Test: Marks a class or a method as a part of the test.

@BeforeMethod: A method which is marked with this annotation will be executed before every @test annotated method.

@AfterMethod: A method which is marked with this annotation will be executed after every @test annotated method.

@BeforeClass: A method which is marked with this annotation will be executed before first @Test method execution. It runs only once per class.

@AfterClass: A method which is marked with this annotation will be executed after all the test methods in the current class have been run

@BeforeTest: A method which is marked with this annotation will be executed before first @Test annotated method.

@AfterTest: A method which is marked with this annotation will be executed when all @Test annotated methods complete the execution of those classes which are inside <test> tag in testng.xml file.

@BeforeSuite: A method which is marked with this annotation will run only once before all tests in the suite have run.

@AfterSuite: A method which is marked with this annotation will run once after execution of all tests in the suite have run

@BeforeGroups: This annotated method will run before the first test run of that specific group.

@AfterGroups: This annotated method will run after all test methods of that group completes its execution.

Some other TestNG Annotations, we need to discuss here are mentioned below:

@Parameters: This annotation is used to pass parameters to test methods.

@DataProvider: If we use @DataProvider annotation for any method that means you are using that method as a data supplier. The configuration of @DataProvider annotated method must be like it always return Object[][] which we can use in @Test annotated method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.

@Factory: Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ].

@Listeners: This annotation is used with test class. It helps in writing logs and results.

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

Benefits of Using TestNG Annotations:

Following are some of the benefits of using annotations:

  • TestNG identifies the methods it is interested in, by looking up annotations. Hence, method names are not restricted to any pattern or format.
  • We can pass additional parameters to annotations.
  • Annotations are strongly typed, so the compiler will flag any mistakes right away.
  • Test classes no longer need to extend anything (such as TestCase, for JUnit 3).

Let’s see the order of methods called using the below script:

Console output:

Execution process is as follows:

First of all, beforeSuite() method is executed only once.

Lastly, the afterSuite() method executes only once.

Even the methods beforeTest(), beforeClass(), afterClass(), and afterTest() methods are executed only once.

beforeMethod() method executes for each test case but before executing the test case.

afterMethod() method executes for each test case but after executing the test case.

In between beforeMethod() and afterMethod(), each test case executes.

This is all about TestNG Annotations. If you have any queries, please comment below. You could find the complete TestNG tutorial here.

 

How To Create TestNG XML File And Execute TestNG.XML File:

Create 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.

Steps to create TestNG XML file 

Step 1: Create testng xml file

i. Right click on Project folder, go to New and select ‘File‘ as shown in below image.

ii. In New file wizard, add file name as ‘testng xml‘ as shown in below given image and click on Finish button.

iii. It will add testng xml file under your project folder.

Step 2 : Write xml code: 

i. Now add below given code in your testng xml file.

Note: You can choose any name for your Test Suite & Test Name as per your need.

ii. After giving appropriate names, now your testng xml file will looks like this:

The hierarchy in the testng xml file is very simple to understand.

Very first tag is the Suite tag<suite>, under that it is the Test tag<test> and then the Class tag<classes>. You can give any name to the suite and the test but you need to provide the correct name to the <classes> tag which is a combination of your Package name and Test Case name.

eg. Package Name is “softwareTestingMaterial”Test Case Name is “STMTestNGClass”. So the Class Name should be softwareTestingMaterial.STMTestNGClass

Step 3 : Execute a testng xml

Now let’s run the xml. Run the test by right click on the testng xml file and select Run As > TestNG Suite.

 

How To Ignore TestNG Test | TestNG Tutorial:

At times we may face some situations where our code is not ready and the test cases written to test a particular method may fail. In such cases, we could use TestNG annotation @Test(enabled = false). This TestNG annotation allows us to ignore testng test.

If a test method is annotated with @Test(enabled = false), then the test case that is not ready to test is ignored.

Now, let’s see how to ignore TestNG test using a script.

Script – Test Case 1:

Script – Test Case 2: I would like to ignore this test from the test execution

testNg.xml file:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

Console Output:

 

Skip TestNG Test | TestNG Tutorial:

How To Skip TestNG Test:

Let’s see how to skip TestNG test deliberately. Sometimes we may face a situation where our test cases might not be ready and we need to skip those tests from running. One way of skipping a test method is by using throw new SkipException() exception.
Scenario: Skip TestNG Test, If a condition met else continue execution.
Let see a sample WebDriver test case example where I have placed SkipException() inside if condition to Intentionally skip that test.
Note: Once SkipException() thrown, remaining part of that test method will not be executed and control will goes directly to next test method execution.
Scenario 1: 
Let me show you how the below program works by commenting the “throw new SkipException()” in the if condition. 

In the output console, we could see “out of loop” from first test method “aSkipTest” and No need to skip this test from second test method “nonSkipTest”

 Output Console:

Scenario 1: 
I will uncomment the “throw new SkipException()” in the if condition. 

 Output Console:

Skip exception thrown and the remaining part of the first test method “aSkipTest” not executed and control reached to second test method “nonSkipTest” and printed the value as “No need to skip this test”
You could find the complete TestNG tutorial here.

TestNG Groups | TestNG Tutorial:

TestNG Groups:

TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Then TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set.  This gives you maximum flexibility in how you partition your tests and doesn’t require you to recompile anything if you want to run two different sets of tests back to back.

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.

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

Script – Test Case 1:

Script – Test Case 2:

testng.xml:

Console Output:

Group of Groups in TestNG Groups:

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:

testng.xml – Group of Groups:

Console Output:

Groups Exclusion:

TestNG allows you to include groups as well as exclude them. You can ignore a group by using the <exclude> tag as shown below:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

For example, it is quite usual to have tests that temporarily break because of a recent change, and you don’t have time to fix the breakage yet. However, you do want to have clean runs of your functional tests, so you need to deactivate these tests but keep in mind they will need to be reactivated.

Console Output:

 

TestNG Exception | TestNG Tutorial:

TestNG Exception:

In this post, I will show how to use TestNG Exception (i.e., expectedExceptions) parameter along with the @Test annotation. TestNG provides an option of tracing the exception handling of code.

Firstly, we see a basic program without using TestNG Exception.

Created a java class, say, TestNGException.java and added an error condition in the method testException().

testng.xml:

Console Output:

As you can see from the test results, testException() method was marked as failed by TestNG during execution.

Now let’s see TestNG Exception in action.

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

The expected exception to validate while running the below test is mentioned using the expectedExceptions attribute value while using the @Test annotation.

testng.xml:

Console Output:

As you can see from the test results, testException() method was marked as passed by TestNG during execution.

 

TestNG Dependencies | TestNG Tutorial:

TestNG Dependencies:

Sometimes, you may need to invoke methods in a test case in a certain order. Here comes TestNG Dependencies into the picture.

TestNG allows you to specify dependencies either with annotations or in XML.

First, we see Dependencies with annotations:

TestNG allows you to specify dependencies either with:

  • Using attribute dependsOnMethods in @Test annotations, OR.
  • Using attribute dependsOnGroups in @Test annotations.

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

testng.xml:

Console Output:

Now we add the dependsOnMethods attribute to the @Test Annotations and execute the same program.

See the script below.

Execute the same testng.xml which was placed above and see the difference in Console Output

Console Output:

Let’s see Dependencies with XML:

 

testng.xml

Console Output:

 

TestNG Parameterization Using XML | TestNG Tutorial:

TestNG Parameterization Using XML:

Parameterized tests allow developers to run the same test over and over again using different values.

There are two ways to set these parameters:

  • with testng.xml
  • with Data Providers

Let’s see passing parameters with testng.xml:

With this technique, 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

Add test method parameterizedTest() to your test class. This method takes a string as input parameter

Add the annotation @Parameters(“browser”) to this method. The parameter would be passed a value from testng.xml, which we will see in the next step.

testng.xml

Here, name attribute represents the parameter name and value represents the value of that parameter. We could use this parameter to the test method parameterizedTest by using @Parameters(“browser”) as mentioned in the above step.

Now let’s see the complete testng.xml file

Console Output:

TestNG will automatically try to convert the value specified in testng.xml to the type of your parameter. Here are the types supported:

  • String
  • int/Integer
  • boolean/Boolean
  • byte/Byte
  • char/Character
  • double/Double
  • float/Float
  • long/Long
  • short/Short.

 

TestNG Parameterization Using DataProviders | TestNG Tutorial:

TestNG Parameterization Using DataProviders

Parameterized tests allow developers to run the same test over and over again using different values.

There are two ways to set these parameters:

  • with testng.xml
  • with Data Providers

Let’s see passing parameters using DataProviders:

Specifying parameters in testng.xml might not be sufficient if you need to pass complex parameters, or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc…). In this case, you can use a Data Provider to supply the values you need to test.  A Data Provider is a method on your class that returns an array of objects.  This method is annotated with @DataProvider:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

Console Output:

You could find the complete TestNG tutorial here.

TestNG Complete Tutorial

If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.

 

Parallel Test Execution In TestNG [Parallel Execution & MultiThreading] | TestNG Tutorial:

Parallel Test Execution In TestNG:

There are situations where we want to run multiple tests with same or different browsers at the same time. In such cases, we can use “parallel” attribute in testng.xml to accomplish parallel test execution in TestNG

Watch: Cross Browser Testing In Selenium using BrowserStack

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.

let us look at example of Parallel Test execution in TestNG.

In the below program, I took two methods. First methods opens Firefox driver and navigate to http://www.softwaretestingmaterial.com and closes the browser. Second methods opens Chrome driver and navigate to the same URL and closes the browser.

testng.xml file without mentioning parallel attribute:

After running the testng.xml using the above mentioned code, first you could see the firefox browser in action and then you can see the chrome driver in action.

To run both the browsers in parallel, use the below code in your testng.xml file.

Once you run the testng.xml using the above code, you could see both the browsers in action at a time.

Here in the above testng.xml file, I have passed parallel=methods and thread-count=2 at the suite level. I would like to execute selenium scripts in parallel in different threads. Most of the times, these two methods will execute in different threads. Thread Id may vary on every run. Here we are just passing thread count but we are not assigning any thread id, assigning thread id will be taken care by your system processor.

 

Testng Asserts – How To Use Assertions In TestNG:

TestNG Asserts

TestNG Asserts are the most frequently used methods while creating Selenium Scripts. Let’s see Assertions in TestNG and where to use them

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.

Let’s see a basic example using TestNG Asserts.

We do verify the title of the webpage using TestNG Asserts.

Here I do take two test conditions. In the first condition, I take a title value correctly and use assertEquals statement and in the second condition, I take incorrect title value to deliberately throw the exception.

Step 1: Open Gmail

Step 2: Verify whether the title matches to the given String. If it matches, go to the email field and type the given text in the sendKeys method else it throws an exception

Note: In this step, I took the actualTitle value correctly in the below script. So it goes to the email filed and type the given text.

Step 3: Again open gmail

Step 4: Verify whether the title matches to the given String. If it matches, go to the email field and type the given text in the sendKeys method else it throws an exception

Note: In this step, I took incorrect actualTitle value. Here due to assertion fails, it throws an exception.

Different TestNG Asserts Statements:

Assert.assertEqual(String actual,String expected) : Asserts that two Strings are equal. If they are not, an AssertionError is thrown.

Parameters:
actual – the actual value
expected – the expected value

Assert.assertEqual(String actual,String expected, String message) : Asserts that two Strings are equal. If they are not, an AssertionError, with the given message, is thrown.

Parameters:
actual – the actual value
expected – the expected value
message – the assertion error message

Assert.assertEquals(boolean actual,boolean expected) : Asserts that two booleans are equal. If they are not, an AssertionError is thrown.

Parameters:
actual – the actual value
expected – the expected value

Assert.assertTrue(condition) : Asserts that a condition is true. If it isn’t, an AssertionError is thrown.

Parameters:
condition – the condition to evaluate

Assert.assertTrue(condition, message) :  Asserts that a condition is true. If it isn’t, an AssertionError, with the given message, is thrown.

Parameters:
condition – the condition to evaluate
message – the assertion error message

Assert.assertFalse(condition) :  Asserts that a condition is false. If it isn’t, an AssertionError is thrown.

Parameters:
condition – the condition to evaluate

Assert.assertFalse(condition, message) : Asserts that a condition is false. If it isn’t, an AssertionError, with the given message, is thrown.

Parameters:
condition – the condition to evaluate
message – the assertion error message

You could find the complete TestNG tutorial here.

 

How To Use Soft Assert In TestNG | TestNG Tutorial:

Before knowing what is Soft Assert, first let’s see what is an Assert and what is the disadvantage in using Assert and why we are moving to Soft Assert.

Asserts are used to perform validations in the test scripts.

There are two types of Assert:

  1. Hard Assert
  2. Soft Assert

When an assert fails the test script stops execution unless handled in some form. We call general assert as Hard Assert

Hard Assert – Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test

The disadvantage of Hard Assert – It marks method as fail if assert condition gets failed and the remaining statements inside the method will be aborted.

To overcome this we need to use Soft Assert. Let’s see what is Soft Assert.

Soft Assert – 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.

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

Let’s see a practical example:

Here I took two methods namely softAssert() and hardAssert().

In the softAssert() method, I have used SoftAssert class and intentionally passing value false in the assertTrue() method to make it fail

In thehardAssert() method, I simply used Assert and intentionally passing parameter value false in the assertTrue() method to make it fail

Execute the above script and see the console output. You could see only one failure.

Console Output:

Below script is same as the first one but just added assertAll() method in the end of the first method (i.e., softAssert()).

Note:  If you forget to call assertAll() at the end of your test, the test will pass even if any assert objects threw exceptions as shown in the above example. So don’t forget to add assertAll()

Execute the above script and see the console output. There are two failures here. Second failure is due to assertAll() method

Console Output:

 

TestNG Listeners – Selenium WebDriver | Selenium Tutorial:

In this post, we see TestNG listeners. Listeners “listen” to the event defined in the selenium script and behave accordingly. The main purpose of using listeners is to create logs. There are many types of listeners such as WebDriver Listeners and TestNG Listeners.

Here in this post, we see TestNG Listeners. Using TestNG listeners we could generate logs and customize TestNG Reports.

Let’s see how to implement TestNG Listeners.

Step 1: Create a Class “ListenerTestNG” to implement ITestListener methods

Mouse hover on ITestListener and import ITestListener Interface and mouse hover on ListenerTestNG and click on “Add unimplemented methods” to add multiple unimplemented methods (without body)

Here, I am implementing onTestSuccessonTestSkippedonTestFailure methods.

Step 2: Create another Class “ListenerTestNGTestCase” and write a script (which ever script you prefer). Else copy paste the below mentioned script.

Step 3: Add the listeners annotation (@Listeners) in the Class “ListenerTestNGTestCase

The complete “ListenerTestNGTestCase” class after adding Listener annotation is mentioned below:

Step 4: Execute the “ListenerTestNGTestCase” class. Methods in class “ListenerTestNG” are called automatically according to the behavior of methods annotated as @Test.

Step 5: Verify the Output in the console. You could find the logs in the console.

If you want to use listeners in multiple classes.

Add the below lines of code in the TestNG.xml file

Final testng.xml file will be like this:

Execute it by right clicking on testng.xml and run as TestNG Suite

If you are not regular reader of SoftwareTestingMaterial.com then I highly recommend you to signup for the free email newsletter using the below link.

 

How To Run Failed Test Cases Using TestNG In Selenium WebDriver:

Run Failed Test Cases Using TestNG in Selenium WebDriver:

At times, test cases may fail while running automated test scripts.  The reason may be anything (say, Network issue, System issue or browser issue) but as an automation tester, you need to execute the test scripts again. Here is a solution to run failed test cases using TestNG in Selenium.

Don’t you want to take a screenshot on failure and attach that in your report?

Yeah, as a tester we need to capture the failed steps. This post guides you on “How To Capture Screenshot and Insert it in Extent Reports”

We could execute the failed test cases in two ways.

Case 1: Execute failed test cases using TestNG in Selenium – By using “testng-failed.xml”

Steps To follow:

  1. After the first run of an automated test run. Right click on Project – Click on Refresh
  2. A folder will be generated named “test-output” folder. Inside “test-output” folder, you could find “testng-failed.xml”
  3. Run “testng-failed.xml” to execute the failed test cases again.

Case 2: Execute failed test cases using TestNG in Selenium – By Implementing TestNG IRetryAnalyzer.

Create a class to implement IRetryAnalyzer. Here I am creating a class (say, RetryFailedTestCases) and implementing IRetryAnalyzer.

RetryFailedTestCases implements IRetryAnalyzer:

Let’s create another class ‘RetryListenerClass’ by Implementing ‘IAnnotationTransaformer’ interface. transform method is called for every test during test run. A simple implementation of this ‘IAnnotationTransformer’ interface can help us set the ‘setRetryAnalyzer’ for ‘ITestAnnotation’. Add the above class name (RetryFailedTestCases.class) in the below program. This interface does its work in run time by adding annotation to the test methods.

RetryListenerClass implements IAnnotationTransformer:

Let us see the example by executing simple tests below. Here I took two test cases say Test1 and Test2.

Testcase 1:

Testcase 2:

As per the lines of code in Test2, it will fail. So it (Test2) will be executed 2 times (we took the maxRetryCnt as 2) in Retry Class. First lets include below mentioned Listener to testng.xml file. Below mentioned syntax is to add Listener for RetryListnereClass

Final testng.xml file should looks like below:

Execute the testng.xml. Here is the output which I got. You could see in the below mentioned result that the Test 2 is executed three times as we have mentioned ‘maxRetryCnt = 2’. Even though we have just 2 tests, we could find total test runs are 4 in the result.

This way we could run failed test cases using TestNG in Selenium.

 

How To Generate TestNG Reports | TestNG Tutorial:

TestNG Reports:

TestNG Reports come in to the picture once we execute the test cases using TestNG. Once we execute test cases using TestNG, it will generate a default HTML report. Let’s see this process of generating TestNG Reports in detail.

I have created a basic script and mentioned below.

It has three methods namely passTest, failTest and skipTest with @Test annotation.

TestNG.xml file:

Execute the testng.xml file and refresh the project. You could see your project similar to the below mentioned image.

Navigate to ‘test-output’ folder. Now you should find a report ”emailable-report.html‘. This is the default report generated by TestNG.

 

How To Run TestNG Using Command Prompt | Software Testing Material:

Goal: To Run TestNG using Command Prompt:

Steps to achieve our goal – Executing TestNG using Command Prompt:

  1. Open Eclipse and create a Java class
  2. Write a Java program
  3. Convert the Java Program into TestNG
  4. Open command prompt
  5. Run the TestNG using command prompt.

Step i: Open Eclipse and create a Java class

Step ii. Keep all the library files in a folder (here I create a folder name “lib”)

Step iii: Write a Java program

Step iv: Convert the Java Program into TestNG

Step v: Open command prompt

Step vi: Run the TestNG using command prompt

Also you could run TestNG using Batch file (.bat file)

Copy the below code and place it in a notepad and save the file using .bat extension

 

30 Most Popular TestNG Interview Questions And Answers | Software Testing Material:

Most Popular TestNG Interview Questions:

In this post, we will see TestNG Interview Questions with Answers. Our main focus is on Selenium TestNG Interview Questions and also we write some Selenium Interview Questions too. Before going ahead, let’s see some unavoidable Interview Questions such as What Are The Reasons For Choosing Software Testing As Your Career and Explain Your Selenium Test Automation Framework. I don’t want to take much time of yours but I couldn’t move further without mentioning about this inevitable question in any interview i.e., Tell Me About Yourself. Click on the link to get some idea on how to answer Tell Me About Yourself. So, Let’s move on to the actual post.

Don’t miss the following posts:

  • 100+ Selenium Interview Questions And Answers
  • Java Interview Questions for Selenium Testers
  • Framework Interview Questions

 

1. What is TestNG?

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

2. What are the advantages of TestNG?

  1. TestNG provides parallel execution of test methods
  2. It allows to define dependency of one test method over other method
  3. It allows to assign priority to test methods
  4. It allows grouping of test methods into test groups
  5. It has support for parameterizing test cases using @Parameters annotation
  6. It allows data driven testing using @DataProvider annotation
  7. It has different assertions that helps in checking the expected and actual results
  8. Detailed (HTML) reports

3. What are the annotations available in TestNG?

@BeforeTest
@AfterTest
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
@BeforeSuite
@AfterSuite
@BeforeGroups
@AfterGroups
@Test

Practical Example

4. Can you arrange the below testng.xml tags from parent to child?

The correct order of the TestNG tags are as follows

5. How to create and run testng.xml 

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.

6. What is the importance of testng.xml file?

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

7. How to pass parameter through testng.xml file to a test case?

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.

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.

Practical Example

8. 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.

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)

For Complete Post

9. What is Soft Assert in TestNG?

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

10. What is Hard Assert in TestNG?

Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test

Practical Example

11. What is exception test in TestNG?

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.

Practical Example

12. 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.

Output:

13. What is Parameterized testing in TestNG?

Parameterized tests allow developers to run the same test over and over again using different values.

There are two ways to set these parameters:

  • using testng.xml – Practical Example
  • using Data Providers – Practical Example

14. How can we create data driven framework using TestNG?

By using @DataProvider annotation,  we can create a Data Driven Framework.

Practical Example

15. How to run a group of test cases using TestNG?

TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Then TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set.  This gives you maximum flexibility in how you partition your tests and doesn’t require you to recompile anything if you want to run two different sets of tests back to back.

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.

Practical Example

TestNG Interview Questions 16 – 33

16. How to create Group of Groups in TestNG?

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:

Practical Example

17. How to run test cases in parallel using TestNG?

we can use “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.

Practical Example

18. How to exclude a particular test method from a test case execution? 

By adding the exclude tag in the testng.xml

19. How to exclude a particular test group from a test case execution? 

By adding the exclude tag in the testng.xml

Practical Example

20. How to disable a test case in TestNG ?

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

21. How to skip a @Test method from execution in TestNG?

By using throw new SkipException()

Once SkipException() thrown, remaining part of that test method will not be executed and control will goes directly to next test method execution.

Practical Example

22. How to Ignore a test case in TestNG?

To ignore the test case we use the parameter enabled = false to the @Test annotation.

Practical Example

23. How TestNG allows to state dependencies?

TestNG allows two ways to declare the dependencies.

Using attributes dependsOnMethods in @Test annotations – Practical Example
Using attributes dependsOnGroups in @Test annotations – Practical Example

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

TestNG offers two ways to produce a report.

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.

25. 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, onTestSuccessonTestFailureonTestSkipped 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

26. How to write regular expression In testng.xml file to search @Test methods containing “smoke” keyword.
Regular expression to find @Test methods containing keyword “smoke” is as mentioned below.
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?

TestNG can be invoked in the following ways

  • Using Eclipse IDE
  • Using ant build tool
  • From the command line
  • Using IntelliJ’s IDEA

29. How To Run TestNG Using Command Prompt?

Run the TestNG using command prompt

Open command prompt and use the below code

30. What is the use of @Test(invocationCount=x)?

The invocationcount attribute tells how many times TestNG should run a test method

In this example, the method testCase1 will be invoked ten times

31. What is the use of @Test(threadPoolSize=x)?

The threadPoolSize attribute tells to form a thread pool to run the test method through multiple threads.

Note: This attribute is ignored if invocationCount is not specified

In this example, the method testCase1 will be invoked from three different threads

32. What does the test timeout mean in TestNG?

The maximum number of milliseconds a test case should take.

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.

33. What are @Factory and @DataProvider annotation?

@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.

@DataProvider: A test method that uses DataProvider will be executed the specific methods multiple number of times based on the data provided by the DataProvider. The test method will be executed using the same instance of the test class to which the test method belongs.

I would like to conclude this post “TestNG Interview Questions” here.

Final words, Bookmark this post “TestNG Interview Questions” for future reference. After reading this post “TestNG Interview Questions”, if you find that we missed some important questions, please comment below we would try to include those with answers.

WhatsApp us