0% found this document useful (0 votes)
46 views

TestNG Tutorial - Annotations, Framework, Examples 2021

Uploaded by

jeffa123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

TestNG Tutorial - Annotations, Framework, Examples 2021

Uploaded by

jeffa123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

5/26/2021 TestNG Tutorial: Annotations, Framework, Examples 2021

INTERVIEW QUESTIONS JAVA JAVA PROGRAMS TEST CASES SELENIUM MANUAL TESTING DIFFERENCE

SoftwareTestingo » Selenium » Selenium Tutorial » TestNG Complete Tutorial

TestNG Complete Tutorial


Last Updated On: June 6, 2020 By Softwaretestingo Editorial Board
Search this website

Tutorials Important Links

Software Testing Jobs

Manual Testing Tutorial

Selenium Tutorial

Core Java Tutorial

TestNG Tutorial

What We Are Learn On This Post  Java Programs

Selenium Programs

Manual Test Cases

Interview Tips

Contact US

www.softwaretestingo.com

Important Links

Software Testing Interview Questions

TestNG Tutorial: This post, we are going to share the complete TestNG tutorials for both fresher and Agile Interview Questions
experienced testers. TestNG is an automation testing framework where NG stands for “Next Generation.”
Manual Testing Interview Questions

Testing Interview Questions

Selenium Interview Questions

Selenium Real Time Interview Questions

Selenium WebDriver Interview Questions

Automation Framework Interview Questions

Appium Interview Questions

https://www.softwaretestingo.com/testng-tutorial/ 1/8
5/26/2021 TestNG Tutorial: Annotations, Framework, Examples 2021

TestNG Interview
INTERVIEW QUESTIONS JAVA JAVA PROGRAMS TEST CASES SELENIUM MANUAL TESTING Questions
DIFFERENCE

Java Interview Questions

Core Java Interview Questions

Categories

Select Category

It is an open-source testing framework, and also it is inspired by the JUnit and NUnit. But TestNG has
introduced some new functionality which makes this framework is more powerful and easy to use.

TestNG Tutorial In Selenium WebDriver


In this tutorial post, we are going to discuss things like:

Why Should we use TestNG


How to Install TestNG
How to Create a first test Case using TestNG
How to Create a Test Suite
Understanding on TestNg.xml
Package Tag in TestNG. Xml

The above topics we are going to discuss one by one in detail. i hope after going through with all those
topics, you have a clear idea, and also, you know how you can use testNG.xml in your Selenium
automation framework with Selenium WebDriver.

Why Should We Use TestNG Framework?


In our previous post, we have written so many automation scripts during the discussion of various topics
of Selenium WebDriver. If you have noticed in all last example, we have written all the script inside the
main method. To execute any particular script, we are executing the specific method of that class.

Let us take a real-time scenario of your automation framework where you can find more then 50+
automation test scripts are available. But let’s think about the below scenarios:

Lets your requirement is run all those test scripts at a time, then, in that case, you need to write a
separate runner class where you can call the main method of each script class. During the execution
of those scripts, if you want to stop or continue your script execution, then its very much
complicated.
Out of all those scripts, if you want to run some specific test scripts or based on some condition if
you’re going to run some particular script, then for that, you need to write manual lots of codes.
Each test cases have some prerequisites and post requisites. To achieve this again, you need to
write lots of code, and some times you need to write the same code repeatedly.
If some scripts are dependent on some other scripts, then its very much complicated to handle this
type of scenario.
If you need to send your execution report after each execution, then for report generation again, you
need to write a bunch of codes.

https://www.softwaretestingo.com/testng-tutorial/ 2/8
5/26/2021 TestNG Tutorial: Annotations, Framework, Examples 2021
The INTERVIEW
above are the QUESTIONS
typical scenario JAVA
that youJAVA
face while writing the TEST
PROGRAMS automation
CASESscripts, and to achieve
SELENIUM this,
MANUAL TESTING DIFFERENCE
we need to spend lots of time and also need extra coding effort. So at that time, if you have found
something which has already had those features and no need for additional coding, then that will save
you lots of time and effort too.

To solve those problems, we can use the TestNG with our script by simply plug-in or adding that with our
automation script. Its just like Selenium, which means by using Selenium WebDriver, we can operate
Launch the browser, loading a URL without writing any extra codes on our own. Like this, TestNG also
comes in the form of Jar. Once we have added to our framework, then we will get lots of predefined
ready-made functionalities, and we can use those functionalities.

Key Points Of TestNG Framework


TestNG is a testing framework which mainly designed for Unit Testing purposes, but today its used
for all types of testing.
When it was develope, that time majorly concentrate on simplifying the different testing like System
Testing or Unit Testing.
TestNG is an open-source framework that is inspired by the JUnit & NUnit.
You can use this for any testing that may be a Web Application Testing or API Testing.

Features of TestNG Framework


TestNG support so many powerful features and also easy to sue. So we have listed some of the TestNG
noteworthy features below:

Logs can be generated.


It provides annotation, which makes the code efficient and easy to manage.
You can include or exclude any test method from execution by using attributes.
Supports parallel testing of the application
Support parameterization, we can achieve this by using the parameter tag.
It Supports Data-Driven Testing using @DataProvider
You can set the priorities so that test methods will be executed in order.
You can easily integrate various tools like build tools (Maven) & different IDE like (Eclipse).
Supported different Annotations like @BeforeSuite, @AfterSuite, @BeforeClass, @AfterClass,
@BeforeTest, @AfterTest, @BeforeGroups, @AfterGroups, @BeforeMethod, @AfterMethod,
@DataProvider, @Factory, @Listeners, @Parameters, @Test.
Advantages of TestNG over JUnit

TestNG most popular then JUnit because its more rich functionality and features, so we have listed
functions below:

TestNG annotation are easy to understand as compare to the JUnit annotations.


There are constraints like you have to declare the @BeforeClass and @AfterClass, but when it
comes to TestNG, there is no such restriction.
You can not be grouping test cases in JUnit, but you can do that with the TestNG.

How to Install TestNG in Eclipse?


We have made a separate post to make this post informative and straightforward. You can check how to
Install TestNG in Eclipse by following this link.

How to Create a First Test Case with TestNG?


There is nothing much different between a Java class and a TestNG class. In TestNg class, you need to
use the @test annotation in the place of the main() method. In Java programming language The JVM will
start the execution from the Main method, but in TestNG, the execution will begin from the @test
annotation. You can follow our article on how to write your first test case using TestNG.

How to Create Test Suite?


Above, we have seen how to create a test case using TestNG. There we have seen how to run the test
case using the “Run as” option. When you want to run a set of test cases together, we can do that by
generating a test suite. Follow this link to learn how you can create a test suite.

Understanding of TestNG.xml
You have seen when we are creating a test suite, an XML file generated. In the XML file, there are five
levels is there & that is:

https://www.softwaretestingo.com/testng-tutorial/ 3/8
5/26/2021 TestNG Tutorial: Annotations, Framework, Examples 2021
Suite
INTERVIEW QUESTIONS JAVA JAVA PROGRAMS TEST CASES SELENIUM MANUAL TESTING DIFFERENCE
Test
Class
Classes
Method

Internal Logic Of Generation Of TestNG.xml


When we are creating the testng.xml file for a class or package or project, you will see that only those
classes are available inside the xml file, which has one or more @test annotated methods.

package TestNGPrograms;
public class NoTestMethod
{
public void doNothing()
{
System.out.println(" Do nothing");
}
}

If we are tr to create an xml file for this class then the XML file should be looking something like below:

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test"></test>
<!-- Test -->
</suite>
<!-- Suite -->

As the above java file does not have any @test annotated method, that’s why the XMl file does not have
a class tag.

If the class have annotation like @BeforeClass or @BeforeSuite etc. in a class then also the xml file
ignore those classes. Because as there is no @test annotated method, then TestNG finds out there is
nothing to test in that class, that’s why it ignores those classes.

Note: So testng.xml will include only those classes which have at least one @Test annotated method in
it.

Package Tag In TestNG


When we are discussing the different tags in the testng.xml file at that time, we have mentioned that there
is a 4 level of tags are available. But apart from those 5, one more tag is present that’s package tag.

As earlier, we have mentioned that if all the classes have the @test annotation and you generate the
testng.xml file, then all the class comes inside the classes tag. Similarly, if there are multiple packages
are present with multiple classes, and you create the testng.xml file also all the class of different
packages comes under one classes tag, but inside the class tag, you will find the class name mentioned
with the respective_packagename.class_name Format.

But the main problem here is a different class of each package is not in order, so it is very much difficult
to find a class from a specific package. To solve such type of problem in TestNG we have another tag
called <packages> & <package> tag.

With the help of using this <packages> & <package> tag we can categorize or group all different package
under <packages> tag and all different classes of a package under <package> tag.

So to create such a testng.xml file with package tag, you need to select the “packages” from the Class
selection drop-down window. Then it will include all the classes of those packages in the test suite.

Note: Here, you will not get any option to select the classes for a run when you are using the package
option.

If you want to include all classes or packages, then you can mention this in the test suite:

For Packages:

<packages>
<package name=".*" />

https://www.softwaretestingo.com/testng-tutorial/ 4/8
5/26/2021 TestNG Tutorial: Annotations, Framework, Examples 2021
</packages>
INTERVIEW QUESTIONS JAVA JAVA PROGRAMS TEST CASES SELENIUM MANUAL TESTING DIFFERENCE

For Classes:

<classes>
<class name=".*" />
</classes>

Note: If you’re going to run all the classes by using a test suite and you have used the above technique,
then you will get a TestNG Exception because it will search for a class with class name .*. So, in that
case, you can use the package tag to run all the classes of a specific package.

How to Deal Sub packages In TestNG?


We created packages created for grouping similar types of classes or interfaces. In the same way, we
can create the sub-packages. But it is important to deal with the sub-packages because when we are
creating the test suite for the main packages, you can find out that it only adds the classes of main
packages and ignoring the sub-packages classes.

Similarly, if you will create a testng.xml file for the sub-package, then it will add only the subpackage
classes and will ignore the main package classes.

So you can add all the classes of the main package and sub-packages in two ways:

By Selecting The Project


By including all package using the <packages> and <package> tag.

By Selecting The Project


For all classes of main package and subpackage, right-click on the project name and generate the
testng.xml file.

By Manually Including Packages


As we have discussed, you can add all the classes manually by mentioning inside the <classes> tag and
the class name mentioning package hierarchy.class_name;

How to Run Inner TestNG Class from the main testNG.xml?


Suppose there is a Java Class which have an inner class, like below:

package InnerClassPackage;
import org.testng.annotations.Test;
public class OuterClass
{
@Test
public void outerMethod()
{
System.out.println("Outer");
}
class InnerClass
{
@Test
public void innerMethod()
{
System.out.println("Inner");
}
}
}

Now let us create a testng.xml for this and try to run. As we can see that we got a TestNGException by
mentioning, “Cannot find the class in classpath.” It means it is not able to find the inner class.

So to handle such types of issues, we can take the help of <package> tag. So let us create a testng.xml
file with the package option and try to run.

How to use Interface With TestNG?


Let’s Create an Interface with @test annotation:

https://www.softwaretestingo.com/testng-tutorial/ 5/8
5/26/2021 TestNG Tutorial: Annotations, Framework, Examples 2021

package TestNGInterface;
INTERVIEW QUESTIONS JAVA JAVA PROGRAMS TEST CASES SELENIUM MANUAL TESTING DIFFERENCE
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public interface InterfaceTestNGMethods
{
// TestNG annotated methods in an Interface
@BeforeTest
public void beforeTestMethod();
@Test
public void testMetho() ;
@BeforeTest
public void afterTestMethod();
}

Let’s create a testng.xml file and run it. As a result, we can say that the result becomes total test run=0,
failures=0, and skips=0.

Now let us update the above interface with the default and static method:

package TestNGInterface;
import org.testng.annotations.Test;
public interface InterfaceTestNGMethods
{
// TestNG annotated methods in an Interface
@Test
public static void staticMethod()
{
System.out.println("Static method");
}

@Test
public default void DefaultMethod()
{
System.out.println("Default Method");
}
}

Now, let’s run the updated interface again and check the result. This time also we are getting the same
result, which is total test run=0, failures=0, and skips=0.

Now we will try to implement the interface in a class, like below:

package TestNGInterface;
import org.testng.annotations.Test;
public class DriverClass implements InterfaceTestNGMethods
{
@Test
public void DriverClassMethod()
{
System.out.println("Driver class method");
}
}

For one more time, prepare the test suite having both interface and implemented class and run that.

For this time, we can see both interfaces and implemented class methods got executed.

Note: An interface can have TestNG annotated methods, but those interface methods can only be
executed once those implemented. If an interface has fully implemented default and static method, also if
you run them, those will be executed, not gives you any errors.

More Articles About TestNG Tutorial:

How to Install TestNG


First Test Case Using @TestNG
Different TestNG Annotation
Different TestNG Attributes
@Test Annotation

https://www.softwaretestingo.com/testng-tutorial/ 6/8
5/26/2021 TestNG Tutorial: Annotations, Framework, Examples 2021
Priority Attributes
INTERVIEW QUESTIONS JAVA JAVA PROGRAMS TEST CASES SELENIUM MANUAL TESTING DIFFERENCE
Timeout Attributes
Always Run Attribute
TestNG expectedException Attribute
TestNG Dependency
Parameterization In TestNG
How to do parallel test execution using TestNG
TestNG Assertions
TestNG Groups
TestNG Inheritance
TestNG Method Overloading
TestNG Overriding
TestNG Method with return Statement
DataProvider in TestNG
@Factory Annotation In TestNG
DataProvider VS Factory
TestNG Listeners
ITestContext in TestNG
TestNG Report
How to Use Extent Report
Execute TestNG Command Prompt

I hope this article helps you know the Basic things about the TestNG. If you enjoy reading this article,
then you can help us by sharing this article link with your friends so that they can also benefit. If there are
any Tips/suggestions or questions, then you can drop in the comment section, and we are happy to reply
ASAP.

Source: link

Filed Under : Selenium Tutorial

Leave a Reply
Your email address will not be published. Required fields are marked *

Comment

https://www.softwaretestingo.com/testng-tutorial/ 7/8
5/26/2021 TestNG Tutorial: Annotations, Framework, Examples 2021

INTERVIEW QUESTIONS JAVA JAVA PROGRAMS TEST CASES SELENIUM MANUAL TESTING DIFFERENCE

Name *

Email *

POST COMMENT

Copyright © 2021 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy

https://www.softwaretestingo.com/testng-tutorial/ 8/8

You might also like