Overview:
If we use testNG framework, it could be difficult to maintain the testNG suite xml for a huge project where we have thousands of test cases. For such projects, it is convenient to maintain the tests in a spreadsheet & create the testNG suite xml from the spreadsheet.
In this article, let’s see how we could create the testNG suite XML file at run time!
Sample Spreadsheet:
Lets assume, I keep all my test cases in a spreadsheet as shown here.
- Description column is used to provide high level description of the test. It is used as the value of the name tag in testNG suite xml.
- There is an Active indicator to run only Active (marked as ‘Y’) test cases. We might not want to run all.
- There is a Module column to group the tests. For ex: All tests related to Order module should be executed.
- ClassName column shows the list of classes (comma separated) to be executed as part of the test.
- Data column (you could add more coulmns for data) is used to pass the data as the parameter to the test in the testNG suite XML.
I would want to query the above spreadsheet to get the specific tests. For example, If I need to execute all the active and order module tests, I would be using SQL as shown here.
Select * from TestCase where Active = 'Y' and Module = 'Order';
Fillo:
To query the spreadsheet, I have been using Fillo for the past few years. It is just a Java API to query xls, xlsx files. The advantage of using Fillo here is, it treats your spreadsheet as a database and each sheet as a table of the database.
Check here for the maven dependency and some sample code to use the library.
Suite Object Implementation:
First lets create a class diagram for the testNG suite xml. We could expect the testNG suite xml in the below format
So, our class hierarchy will be as shown here.
suite (name) test (name) parameter (name, value) classes class (name)
For the above model, test, class, parameter could be Lists.
I add the below mvn dependencies in my java project.
I create a Suite.java to represent the above class hierarchy – I would create an instance of this class by reading the spreadsheet, so that I could export this as a testNG xml.
XLS Reader:
I create a sample XLS reader as shown here using Fillo. I query the spreadsheet and then create the xml.
TestNG Suite XML:
Simply executing specific query will fetch corresponding tests and create a testng xml at run time as shown here.
testng xml:
Summary:
Above approach for creating testNG suite xml at run time saves a lot of time in maintaining the suite file for big projects where we could have thousands of test cases. We maintain all the tests in a spreadsheet and simply tag the module names to logically group the tests. So that I could use SQL to filter specific tests I need to execute.
Happy Testing & Subscribe