TestNG Tutorials 10: What Is Package Tag And How To Use In TestNG.XML?

Hello Folks,

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 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 tag with multiple 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 and tags.

All class names of each package are grouped within separate package tag first and then all package names are combined within tag. tag is a container which contains multiple tags. It is similar to nesting of and 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 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 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. #ThanksForReading

#HappySelenium