Comparaison
Similarities
| Feature | JUnit4 | TestNG |
|---|---|---|
| test annotation | @Test | @Test |
| test method setUp equivalent | @Before | @Configuration(beforeTestMethod=true) |
| test method tearDown equivalent | @After | @Configuration(afterTestMethod=true) |
| test class setUp | @BeforeClass | @Configuration(beforeTestClass=true) |
| test class tearDown equivalent | @AfterClass | @Configuration(afterTestClass=true) |
| ignore test | @Ignore | @Test(enabled=true) |
JUnit4
- Backward compatible. Run your JUnit3 test through JUnit4 and run JUnit4 as JUnit3 tests in all tools (IDE, ant...)
TestNG
- Suites are optionally defined in an xml file testng.xml
- Grouping of test: categorize your test through annotation. You can then have in one test class tests that run in different suites like check-in, db, functional... This is nice to keep all your unit and integration tests pertaining to one class in the same test class. For example TestUserStory could contain
- tests that test the hibernate mapping and the db queries. These would run in the db or integration suites
- tests that test the domain logic of the class. These would run in the unit or checkin suites
- Run last failures
- 2 additional setUp/tearDown level: suite and test (controlled in the testng.xml)
I am still not sure that the introduction of an XML definition of suites and test is a good thing. I understand the fact that you don't have to recompile to change a suite but how much pain does compiling one file cause?