TestNG vs JUnit: cease fire or the end of the war
-
Upload
oleksiy-rezchykov -
Category
Technology
-
view
8.104 -
download
2
description
Transcript of TestNG vs JUnit: cease fire or the end of the war
![Page 1: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/1.jpg)
JUnit vs TestNG:ceasefire or the end of the war?
Oleksiy RezchykovOctober 2013
![Page 2: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/2.jpg)
@twincengray #xpdays_ua
Who am I?• Software Engineer • Last 6 years working with Java• XP/Agile/Lean practitioner• Lazy Pragmatic programmer
![Page 3: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/3.jpg)
@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
![Page 4: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/4.jpg)
@twincengray #xpdays_ua
TDD
![Page 5: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/5.jpg)
@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
![Page 6: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/6.jpg)
@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
![Page 7: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/7.jpg)
@twincengray #xpdays_ua
Runners: JUnit
![Page 8: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/8.jpg)
@twincengray #xpdays_ua
Runners: JUnit
• By default BlockJUnit4ClassRunner is used• You could add your own features by extending it
![Page 9: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/9.jpg)
@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
![Page 10: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/10.jpg)
@twincengray #xpdays_ua
Cycle: JUnitBefore/after class @BeforeClass/
@AfterClass (static methods)
Before/after test @Before/@After
![Page 11: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/11.jpg)
@twincengray #xpdays_ua
Cycle: TestNGBefore/after suite @BeforeSuite/
@AfterSuite
Before/after class @BeforeClass/@AfterClass
Before/after test @BeforeMethod/@AfterMethod
![Page 12: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/12.jpg)
@twincengray #xpdays_ua
Suites: JUnit
![Page 13: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/13.jpg)
@twincengray #xpdays_ua
Suites: TestNGUsing testng.xml and <suite> tag
![Page 14: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/14.jpg)
@twincengray #xpdays_ua
Groups: JUnit
![Page 15: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/15.jpg)
@twincengray #xpdays_ua
Groups: TestNG@BeforeSuite/@AfterSuite@BeforeTest/@AfterTest@BeforeGroups/@AfterGroups@BeforeClass/@AfterClass@BeforeMethod/@AfterMethodAll has groups attribute
![Page 16: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/16.jpg)
@twincengray #xpdays_ua
Groups: TestNGYou can define which groups you want to run using testng.xml
![Page 17: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/17.jpg)
@twincengray #xpdays_ua
Test dependenciesJUnit:Since Junit 4.11 @FixMethodOrder(MethodSorters.JVM)
TestNG:@Test attributes dependsOnGroups, dependsOnMethods
![Page 18: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/18.jpg)
@twincengray #xpdays_ua
Parameterized tests: JUnit
![Page 19: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/19.jpg)
@twincengray #xpdays_ua
Parameterized tests: TestNG
The values could be passed from testng.xml
![Page 20: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/20.jpg)
@twincengray #xpdays_ua
Parameterized tests: TestNG
@DataProvider annotation could be used.
![Page 21: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/21.jpg)
@twincengray #xpdays_ua
Exceptions: JUnit• Try/catch with fail()• Expected attribute of the @Test annotation• ExpectedException as a @Rule
![Page 22: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/22.jpg)
@twincengray #xpdays_ua
Exceptions: TestNG
![Page 23: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/23.jpg)
@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)
![Page 24: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/24.jpg)
@twincengray #xpdays_ua
Test run timeout• TestNG - attribute in @Test annotation• JUnit – attribute in @Test annotation
![Page 25: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/25.jpg)
@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
![Page 26: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/26.jpg)
@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
![Page 27: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/27.jpg)
@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
![Page 28: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/28.jpg)
@twincengray #xpdays_ua
To readTestNG:• http://testng.org/• http://testng.org/doc/book.html• http://beust.com/
JUnit:• https://github.com/junit-team/junit/wiki
![Page 29: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/29.jpg)
@twincengray #xpdays_ua
Presentation and code• http://www.slideshare.net/mcgray
• https://github.com/mcgray/junit-vs-testng
![Page 30: TestNG vs JUnit: cease fire or the end of the war](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f3e67b4c90572088b5160/html5/thumbnails/30.jpg)
@twincengray #xpdays_ua
Questions?@twincengray
http://McGray.com.ua