Since I launched this site, I have done a few tutorials on the two major unit testing frameworks in the Java space: JUnit and TestNG (you can view them in the Tutorials section of this site). Each of them are very competent frameworks that provide a robust set of features that allow developers to create complex test scenarios to adequately test just about any piece of code. But if you are starting a new project, which should you choose? If you already have a suite of test cases in one or the other, should you switch? Let’s find out.
|User Defined Life Cycle|
|Test Organization (groups, etc)|
|Distributed Test Execution|
|Parallel Test Execution|
|Data Driven Tests|
|Domain Specific Extensions (Database, HTTP, etc)|
The criteria I looked at, as you can see above was pretty diverse. User defined life cycle is the ability to tell the test runner what methods to execute in what order. Both JUnit and TestNG allow you to do this via annotations. The ability to organize my tests is another nice to have. TestNG allows you to group tests across test classes via annotations. This is a handy tool for crateing groups for different development tasks (checking in, continuous integration build, by functional area, etc) that JUnit doesn’t have. The most grouping you can do with JUnit is by grouping test methods together in a single class. On a large project the ability to run tests in either a distributed mode or in a parallel mode can definitely speed up execution. Yet another two points for TestNG. We have all come across very data intensive areas that require a large amount of data to truly test all scenarios. TestNG’s DataProvider method allows for the ability to build a data set for a single test scenario. The scenario is then executed over all of the data. Very powerful stuff. Dependency testing is also something that is important in a large system. I know these are both technically “unit test” frameworks, but they still should be able to handle all testing scenarios we throw at it. Given JUnit’s rigid enforcement of testing in isolation, it is difficult to test dependencies. Yet another point to TestNG.
Both frameworks integrate well into all major IDEs as well as the major build tools of the day. JUnit has a large collection of third party add ons that can help in domain specific areas like database testing and HTTP front end testing. However since TestNG can execute JUnit tests, you don’t loose the ability to use those tools by using TestNG. They both have an active community with a plethora of documentation available on the net (although at the time of this writing, JUnit.org has been down for a few days. Not sure what is going on there).
For me, the decision is clear. TestNG is a high caliber testing framework that can assist in just about any testing scenario you can think of. The guys over at TestNG did a good job. They originally intended to take JUnit and make it better. Given the easy learning curve and powerful features, it’s hard to argue against TestNG as the way to go in unit testing.