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 <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.
#ThanksForReading
#HappySelenium

 

 

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

  1. Hi Amod,

    Thanks for this article. so if we have multiple packages lets say 3 packages (A,B,C) so execution of those packages will happen from Top to bottom? means i want to execute all classes of A Package First, then all classes of B Package and at last for for C Package. will this ensure order of execution.

    Thanks,
    Shreyansh

    1. Hi Shreyansh,
      Preserver-order is an attribute to do so which you can set as true at test tag.

      1. preserve-order attribute is true only by default. I tried using packages and it is not executing all the classes inside the package in sequence.

        1. Either put priorities in class methods or include class tag in stead of package. Perserver-order preserves what you provide in testng xml.

Leave a Reply

Your email address will not be published. Required fields are marked *