When we generate a testng.xml for TestNG classes of a package, we see all class names (with at least one @Test annotated method) of that package under “class” tag wrapped by <classes> tag.
We can see an example below:
When we have multiple packages in a project , testng.xml includes all TestNG classes from all packages within single <classes> tag with multiple <class> tags as below:
You can learn more about generation of testng.xml in this post.
In above testng.xml you can see every class name is prefixed by package name i.e. packageName.className which is also not in order. All classes from all packages are mixed in testng.xml as you can see in above pic. If there are many packages, you may be confused. Suppose, if you need to remove all classes of specific package, it will be difficult. So for better categorization, TestNG provides you <package> and <packages> tags.
All class names of each package are grouped within separate package tag first and then all package names are combined within <packages> tag. <packages> tag is a container which contains multiple <package> tags. It is similar to nesting of <classes> and <class> tag.
To generate a testng.xml using package tag, you just need to select “Packages” in Class selection drop-down in testng.xml generation window as shown below:
It will include all classes of those packages for testng suite. Remember here we do not have option to select classes to run when we use package tag. You lose that flexibility here.
If you want to include all packages, TestNG allows you to use expressions in stead of multiple <package> tags as below:
You just need to use star (*) with dot(.). Dot indicates current project directory while star represents all package names in current directory.
Note: You can not use same concept in case of classes as below:
If you run above testng.xml you will get org.testng.TestNGException: Cannot find class in classpath exception because it will search for a class with name “.*”.
So, if you want to run all classes of all packages in a Project, it is good to use <package> tags.
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 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw 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