JUnit vs TestNG:
ceasefire or the end of
the war?
Oleksiy Rezchykov
October 2013
Who am I?
• Software Engineer
• Last 6 years working with Java
• XP/Agile/Lean practitioner
• Lazy Pragmatic programmer
@twincengray #xpdays_ua
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
TDD
@twincengray #xpdays_ua
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
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
Runners: JUnit
@twincengray #xpdays_ua
Runners: JUnit
• By default BlockJUnit4ClassRunner is used
• You could add your own features by extending it
@twincengray #xpdays_ua
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
Cycle: JUnit
Before/after class @BeforeClass/@AfterClas
s (static methods)
Before/after test @Before/@After
@twincengray #xpdays_ua
Cycle: TestNG
Before/after suite @BeforeSuite/@AfterSuit
e
Before/after class @BeforeClass/@AfterClas
s
Before/after test @BeforeMethod/@AfterM
ethod
@twincengray #xpdays_ua
Suites: JUnit
@twincengray #xpdays_ua
Suites: TestNG
Using testng.xml and <suite> tag
@twincengray #xpdays_ua
Groups: JUnit
@twincengray #xpdays_ua
Groups: TestNG
@BeforeSuite/@AfterSuite
@BeforeTest/@AfterTest
@BeforeGroups/@AfterGroups
@BeforeClass/@AfterClass
@BeforeMethod/@AfterMethod
All has groups attribute
@twincengray #xpdays_ua
Groups: TestNG
You can define which groups you want to run using
testng.xml
@twincengray #xpdays_ua
Test dependencies
JUnit:
Since Junit 4.11
@FixMethodOrder(MethodSorters.JVM)
TestNG:
@Test attributes dependsOnGroups,
dependsOnMethods
@twincengray #xpdays_ua
Parameterized tests: JUnit
@twincengray #xpdays_ua
Parameterized tests: TestNG
The values could be passed from testng.xml
@twincengray #xpdays_ua
Parameterized tests: TestNG
@DataProvider annotation could be used.
@twincengray #xpdays_ua
Exceptions: JUnit
• Try/catch with fail()
• Expected attribute of the @Test annotation
• ExpectedException as a @Rule
@twincengray #xpdays_ua
Exceptions: TestNG
@twincengray #xpdays_ua
@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
Test run timeout
• TestNG - attribute in @Test annotation
• JUnit – attribute in @Test annotation
@twincengray #xpdays_ua
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
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
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
To read
TestNG:
• http://testng.org/
• http://testng.org/doc/book.html
• http://beust.com/
JUnit:
• https://github.com/junit-team/junit/wiki
@twincengray #xpdays_ua
Presentation and code
• http://www.slideshare.net/mcgray
• https://github.com/mcgray/junit-vs-testng
@twincengray #xpdays_ua
Questions?
@twincengray #xpdays_ua
@twincengray
oleksiy.rezchykov@gmail.com
http://McGray.com.ua

TestNG vs JUnit: cease fire or the end of the war