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

14,858 views

Published on

My talk from XPDays Ukraine 2013 about JUnit and TestNG.

Published in: Technology, Education

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

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

×