Java allows you to group similar types of classes, interfaces and sub packages. You can create nested packages means package inside package which is termed as sub-packages.
Let’s create a below hierarchy of package:
- Create a Package “MainPackage”.
- Create a class “MainPackageClass” in “MainPackage”
- Create a sub package “SubPackage” inside “MainPackage”.
- Create a class “SubPackageClass” in “SubPackage”.
- Create another sub package “SubOfSubPackage” inside “SubPackage”.
- Create a class “SubOfSubPackageClass” inside “SubOfSubPackage”.
You will see above hierarchy as below:
When you select “MainPackage” package and try to generate a testng.xml, you can see that it includes classes from outer package only. Not from sub packages.
Similarly, if you select any sub package and try to create a testng.xml, you will see classes of that sub packages only not from parent package or sub package of sub package.
So, how can we make a test suite which will include all classes from package and sub packages?
You have two ways to do so.
- Select the Project and create testng.xml. It will include all class names from all packages and sub packages.
Problem with this approach is that it will include all other packages also which you may not want to run.
2. You can use regular expressions with package name. You can take help of <package> tag.
A. To include all sub packages of a package: <package name=”MainPackage.*”/>
Star(*) will include all subpackages of MainPackage i.e. “MainPackage”, “SubPackage” and “SubOfSubPackage”. It will include classes “MainPackageClass”, “SubPackageClass” and “SubOfSubPackageClass.java” classes.
B. To include all classes and sub packages of a sub package: <package name=”MainPackage.SubPackage.*”/>
This will include “SubPackageClass” and “SubOfSubPackageClass.java” classes.
So, this post describes the another advantage of package tag and use of regular expression in testng.xml with package tag.
More about TestNG in upcoming posts. Stay tuned.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
Author: Amod Mahajan
My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning