The document compares the Java unit testing frameworks JUnit and TestNG. JUnit was one of the first unit testing frameworks for Java but TestNG was created later to provide more out-of-the-box features. Both frameworks can be used for unit testing but TestNG provides more support for test grouping, parallelization, data-driven and parameterized tests. While JUnit is sufficient for basic unit testing, TestNG has additional features that make it more suitable for integration and acceptance testing scenarios.
2. Who am I?
• Software Engineer
• Last 6 years working with Java
• XP/Agile/Lean practitioner
• Lazy Pragmatic programmer
@twincengray #xpdays_ua
3. Unit testing
• A method to test smallest possible portion of source
code
• Tests are independent
• Tests are not affecting the state of the application
under test
• Main building block of the TDD
@twincengray #xpdays_ua
5. JUnit
• Canonical unit testing framework for Java
• Originated from SUnit (SmallTalk)
• One of the first in xUnit family
• Developed by Kent Beck and Erich Gamma
• First versions at year 2000
• Now on GitHub
@twincengray #xpdays_ua
6. TestNG
• Test Next Generation
• Alternative unit testing tool for Java
• Authored by Cédric Beust
• First version released in 2004
• First version has a lot of out-of-the-box features
which was missing in Junit
@twincengray #xpdays_ua
8. Runners: JUnit
• By default BlockJUnit4ClassRunner is used
• You could add your own features by extending it
@twincengray #xpdays_ua
9. Runners: TestNG
• TestRunner is used by default
• You can start it with classes or xml configuration
files
• Usually you create base class for tests to add custom
features
• A variety of interceptor interfaces is at your service
@twincengray #xpdays_ua
10. Cycle: JUnit
Before/after class @BeforeClass/@AfterClas
s (static methods)
Before/after test @Before/@After
@twincengray #xpdays_ua
11. Cycle: TestNG
Before/after suite @BeforeSuite/@AfterSuit
e
Before/after class @BeforeClass/@AfterClas
s
Before/after test @BeforeMethod/@AfterM
ethod
@twincengray #xpdays_ua
23. @Rule
• JUnit feature
• Supported by runners which extends
BlockJUnit4ClassRunner
• Framework extension point
• There is a set of implemented rules (e.g.
ExpectedException)
@twincengray #xpdays_ua
24. Test run timeout
• TestNG - attribute in @Test annotation
• JUnit – attribute in @Test annotation
@twincengray #xpdays_ua
25. Running tests in parallel
JUnit:
• Using maven-surefire-plugin
• Gradle Test task attribute maxParallelForks
TestNG:
• <suite> tag has parallel and thread-count attributes
• @Test has threadPoolSize attribute
@twincengray #xpdays_ua
26. Env. Support
• Eclipse plugin (does not come out-of-the-box)
• IDE plugin (out-of-the-box)
• Gradle support
• Maven support
• Framework support
@twincengray #xpdays_ua
27. Conclusion
• JUnit has everything for the UNIT testing but could
be extended to do barely everything
• TestNG has more stuff out-of-the-box and is more
“QA oriented”
• Feature comparison table (from RebelLabs):
http://zeroturnaround.com/wp-
content/uploads/2013/07/unit-testing.jpg
@twincengray #xpdays_ua