Andrew Glover compared TestNG and JUnit 4
, taking a look at some features that TestNG has over JUnit 4. Andrew quickly takes the position that TestNG is better for large scale testing, despite JUnit 4's recent addition of annotations and "dramatically relaxed structural rules for test case authoring." The three killer features that TestNG has over JUnit 4, according to Andrew (summarized here), are:
- Flexibility. TestNG doesn't require public init() methods or static test fixtures.
- Dependency Testing. In JUnit, for tests that fail, they have an inconvenient consequence: every subsequent dependent test also fails. TestNG models the notion of dependencies between tests so that tests dependent on the failed one will be skipped, allowing the rest to proceed. "TestNG's trick of skipping, rather than failing, can really take the pressure off in large test suites."
- Re-Running failed tests. TestNG notes which tests failed and allows you to re-run them instead of all of them - handy on large test suites.
- Parametric Testing. In JUnit, if you want to vary the parameter groups to a method under test, you are forced to write a test case for each unique group. You can use FIT to drive tests with tabular data, but TestNG provides a similar feature right out of the box. You can even feed in complex objects with some additional configuration.
The author conludes:
As I said at the beginning of this column, JUnit 4 and TestNG are similar on the surface. But whereas JUnit is designed to hone in on a unit of code, TestNG is meant for high-level testing. Its flexibility is especially useful with large test suites, where one test's failure shouldn't mean having to rerun a suite of thousands. Each framework has its strengths, and there's nothing stopping you from using both in concert.
Recently, build server vendor Zutubi blogged about how the the dependency test feature in TestNG is a good solution for managing tests with occasionally available external dependencies
. Back in May, the GlassFish project quality
team compared both frameworks
and also chose TestNG as the main test suite for Sun's GlassFish testing. TestNG 5 was released on July 20th.
InfoQ Chief Architect Alexandru Popescu is the co-founder of TestNG 5 along with Cedric Beust.