TestNG Tutorial - Annotations, Framework, Examples 2021
TestNG Tutorial - Annotations, Framework, Examples 2021
INTERVIEW QUESTIONS JAVA JAVA PROGRAMS TEST CASES SELENIUM MANUAL TESTING DIFFERENCE
Selenium Tutorial
TestNG Tutorial
Selenium Programs
Interview Tips
Contact US
www.softwaretestingo.com
Important Links
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
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
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.
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.
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.
TestNG most popular then JUnit because its more rich functionality and features, so we have listed
functions below:
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
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:
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.
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.
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:
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.
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.
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.
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
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
https://www.softwaretestingo.com/testng-tutorial/ 8/8