JUnit vs. TestNG
Friday, December 7th, 2007The problem: We have several XML transformations, which we want to test with several inputs each. The tests are done by running the transformation on a source XML file and comparing the result with an expected XML file (using XMLUnit’s Diff class). These XMLs are stored in directories in the file system, where each such directory contains the input file, the expected result and info of which transformation to use them against.
I want each directory to be treated as a separate unit test, but I don’t want to actually write a separate test method for that. Thus the XML test will be well integrated with the rest of the unit test of the system and I don’t have to develop separate reports and other management stuff for them.
I couldn’t find anything like that for JUnit, other than the advise to read JUnit’s source code and try to figure out where to hook my changes. But I also found this blog entry, discussing JUnit extensions. Yes, I know, his needs were different than mine. Anyway, I decided to give TestNG a try, because I’ve also considered it before for a different project. And guess what I found! They have a nice little feature, called “factories,” which allow you to dynamically generate test cases. Just what I needed.
There are TestNG plugins for various IDEs, including Eclipse, and it is supported by Maven. So, it seems TestNG is the right answer for this situation.