testng vs junit: cease fire or the end of the war

30
JUnit vs TestNG: ceasefire or the end of the war? Oleksiy Rezchykov October 2013

Upload: oleksiy-rezchykov

Post on 10-May-2015

8.098 views

Category:

Technology


2 download

DESCRIPTION

My talk from XPDays Ukraine 2013 about JUnit and TestNG.

TRANSCRIPT

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

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

@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

@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

@twincengray #xpdays_ua

TDD

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

@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

@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

@twincengray #xpdays_ua

Runners: JUnit

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

@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

@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

@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

@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

@twincengray #xpdays_ua

Suites: JUnit

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

@twincengray #xpdays_ua

Suites: TestNGUsing testng.xml and <suite> tag

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

@twincengray #xpdays_ua

Groups: JUnit

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

@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

@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

@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

@twincengray #xpdays_ua

Parameterized tests: JUnit

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

@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

@twincengray #xpdays_ua

Parameterized tests: TestNG

@DataProvider annotation could be used.

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

@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

@twincengray #xpdays_ua

Exceptions: TestNG

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

@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

@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

@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

@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

@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 29: TestNG vs JUnit: cease fire or the end of the war

@twincengray #xpdays_ua

Presentation and code• http://www.slideshare.net/mcgray

• https://github.com/mcgray/junit-vs-testng