TestNG Tutorial: This post will share the complete Java Selenium TestNG Framework for both fresher and experienced testers. TestNG is an automation testing framework where NG stands for “Next Generation.”
It is an open-source testing framework, and JUnit and NUnit inspire it. But TestNG has introduced some new functionality, which makes this framework more powerful and easy to use.
TestNG Tutorial In Selenium WebDriver
In this tutorial post, we are going to discuss things like:
We are going to discuss all the above topics in detail. After going through all those topics, you will have a clear idea and know how to use testNG.xml in your Selenium automation framework with Selenium WebDriver.
Why Should We Use TestNG Framework?
Previously, we have written many automation scripts while discussing various topics of Selenium WebDriver. As you have noticed in all the examples, we have written all the scripts using 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 over 50+ automation test scripts. But let’s think about the below scenarios:
- Suppose your requirement is to 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 it’s very complicated.
- Out of all those scripts, if you want to run specific test scripts or, based on some condition, then we need to write lots of codes.
- Each test case has some prerequisites and post-requisites. To achieve this again, you need to write lots of code; sometimes, you must write the same code repeatedly.
- If some scripts depend on others, handling this scenario is very complicated.
- If you need to send your execution report after each execution, then for report generation again, you need to write a bunch of codes.
The above-discussed scenarios are the typical scenarios you face while writing automation scripts, and to achieve this, we need to spend lots of time and extra coding effort.
But if you have found something with those features without additional coding, that will save you lots of time and effort.
To solve those problems, we can use TestNG with our script by simply plugging in or adding that with our automation script. It is 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 a Jar. Once we have added to our framework, we will get many predefined ready-made functionalities, which we can use.
Key Points Of TestNG Framework
- TestNG is a testing framework mainly designed for Unit Testing purposes, but today, it is used for all types of testing.
- When it was developed, that time majorly concentrated 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 Web Application Testing or API Testing.
Features of TestNG Framework
TestNG supports many powerful features and is also easy to use. 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 to execute test methods in order.
- You can easily integrate tools like build tools (Maven) & different IDEs 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 is more popular than JUnit because of its richer functionality and features. Below we have listed functions below:
- TestNG annotations are easier to understand than JUnit annotations.
- There are constraints like you have to declare the @BeforeClass and @AfterClass in JUnit, but there is no such restriction regarding TestNG.
- You can not perform 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 do you create a first test case with TestNG?
There is nothing much different between a Java class and a TestNG class. In the TestNg class, you must use the @test annotation instead 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 a Test Suite?
Above, we have seen how to create a test case using TestNG. We have seen how to run the test case using the “Run as” option. We can generate a test suite when you want to run a set of test cases together.
Follow this link to learn how to create a test suite.
Understanding of TestNG.xml
You have seen that an XML file is generated when we create a test suite. In the XML file, there are five levels is there & that are:
Internal Logic Of Generation Of TestNG.xml
When creating the testng.xml file for a class, 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 trying to create an xml file for this class, then the XML file should look something like the 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 has no @test annotated method, the XMl file does not have a class tag.
If the class has annotations like @BeforeClass or @BeforeSuite, etc., then the xml file ignores those classes.
Because there is no @test annotated method, TestNG finds out there is nothing to test in that class, so it ignores those classes.
Note: testng.xml will include only those classes with at least one @Test annotated method.
Package Tag In TestNG
When we were discussing different tags in the testng.xml file at that time, we mentioned that there are 5 levels of tags available. But apart from those 5, one more tag is present: the package tag.
Earlier, we mentioned that if all the classes have the @test annotation and if you generate the testng.xml file, then all the classes come inside the classes tag.
Similarly, if multiple packages are present with multiple classes, and you create the testng.xml file, all the class of different packages comes under one class tag, but inside the class tag, you will find the class name mentioned with the respective_packagename—class_name Format.
Example:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test thread-count="5" name="Test"> <classes> <class name="com.softwaretestingo.testng.FirstTestCase"/> <class name="com.softwaretestingo.testng.OrderOfAnnotation"/> <class name="com.softwaretestingo.testng.attributes.Enabled"/> <class name="com.softwaretestingo.testng.attributes.DependsOnMethods"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
But the main problem here is that the different classes of each package are not in order, so it is very 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 packages under the <packages> tag and all different classes of a package under the <package> tag.
So, to create such a testng.xml file with a 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 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=".*" /> </packages>
For Classes:
<classes> <class name=".*" /> </classes>
Note: If you run all the classes using a test suite and have used the above technique, you will get a TestNG Exception because it will search for a class with the 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 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 creating the test suite for the main packages, you can find out that it only adds the classes of main packages and ignores the sub-packages classes.
Similarly, creating a testng.xml file for the sub-package will add only the subpackage classes and ignore the main package classes.
But you can add all the classes of the main package and sub-packages in two ways:
- By Selecting The Project
- Include all packages using the <packages> and <package> tags.
By Selecting The Project
For all main package and subpackage classes, 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 the 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 it. As we can see, 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 it.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test thread-count="5" name="Test"> <packages> <package name="com.softwaretestingo.testng.innerclass"/> </packages> </test> <!-- Test --> </suite> <!-- Suite -->
How to use Interface With TestNG?
Let’s Create an Interface with @test annotation:
package com.softwaretestingo.testng.interfaceex; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public interface InterfaceWithTestMethods { @BeforeTest public void beforeTestMethod(); @Test public void testMethod(); @BeforeTest public void afterTestMethod(); }
Let’s create a testng.xml file and run it. As a result, we can say that the result becomes:
[RemoteTestNG] detected TestNG version 7.8.0 =============================================== Suite Total tests run: 0, Passes: 0, Failures: 0, Skips: 0 ===============================================
Now let us update the above interface with the default and static method:
package com.softwaretestingo.testng.interfaceex; import org.testng.annotations.Test; public interface InterfaceWithTestMethodBody { @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, we are getting the same result, which is:
[RemoteTestNG] detected TestNG version 7.8.0 =============================================== Suite Total tests run: 0, Passes: 0, Failures: 0, Skips: 0 ===============================================
Now we will try to implement the interface in a class, like below:
package com.softwaretestingo.testng.interfaceex; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class InterfaceImplementationClass implements InterfaceWithTestMethods { @Test public void DriverClassMethod() { System.out.println("Driver class method"); } @Override @BeforeTest public void beforeTestMethod() { } @Override @Test public void testMethod() { } @Override @AfterTest public void afterTestMethod() { } }
For one more time, prepare the test suite to have both the interface and implemented class and run that.
We can see both interfaces and implemented class methods were executed for this time.
[RemoteTestNG] detected TestNG version 7.8.0 Driver class method =============================================== Suite Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
Note: An interface can have TestNG annotated methods, but those interface methods can only be executed once they are implemented.
If an interface has fully implemented the default and static methods, if you run them, those will be executed, not giving you any errors.
More Articles About TestNG Tutorial:
Conclusion:
I hope this article helps you know the basics of TestNG. If you enjoy reading this article, you can help us by sharing this article link with your friends so they can also benefit.
If there are any tips/suggestions, or questions, you can drop them in the comment section, and we will be happy to reply ASAP.