Automation QA Testing Course Content

JUnit vs. TestNG

 


1.
Junit  originally developed by kent beck and erich gamma . in 2013

testng, which was created by cedric beust

2.

groups

testng offers additional annotations to those available in junit. probably the most notable is the ability to run code before / after groups of test cases. also, single tests can belong to multiple groups and then run in different contexts (like slow or fast tests). it’s practically another layer in between a test case and a test suite:


a similar feature exists in junit categories but lacks the @beforegroups / @aftergroups testng annotations that allow initializing the test / tearing it down. btw, looks like junit 5 is going to deprecate categories and introduce a new concept that will be called a tag:


but as it looks right now, no @beforetag / @aftertag annotations in sight.


3.parallelism

if you’d like to run the same test in parallel on multiple threads, testng has you covered with a simple to use annotation while junit doesn’t offer a simple way to do so out of the box. the testng implementation would look like:


meaning, three threads and nine invocations of the same test. you can also run whole suites in parallel if you specify it in testng’s xml run configurations. while with junit you’d have to write a custom runner method, and feed the same testing parameters multiple times. which brings us to the next bullet point.


4.parameterized/data-driven testing

this is the problem of feeding different test inputs to the same test case, which both testng and junit solve, but use different approaches. the basic idea is the same, creating a 2d array, object[][] that includes the parameters.

however, in addition to supplying the parameters through the code, the testng @dataprovider can also support xml for feeding in data, csvs, or even plain text files.

a feature that exists in junit and misses on testng is the ability to use different combinations between several arguments. this provides a shortcut to long parameter list and explained in junit theories .

5.dependencies between groups/methods

since junit was built for unit tests, and testng had a wider array of tests in mind, they also differ in their approach to dependencies between tests.

testng allows you to declare dependencies between tests, and skip them if the dependency test didn’t pass:


this functionality doesn’t exist in junit, but can be emulated using assumptions . a failed assumption results in an ignored test, which is skipped.

bottom line: different developers would have different expectations from their framework of choice. testng seems to provide greater flexibility out of the box compared to junit.

5. reporting on the results

test results interest a lot of people. it’s not just the developer who runs them. this is when reports come into play and both frameworks have an answer for this issue.

testng reports are generated by default to a test-output folder that includes html reports with all of the test data, passed/failed/skipped, how long did they run, which input was used and the complete test logs. in addition, it also exports everything to an xml file which can be used to construct your own report template.

on the junit front, all of this data is also available via xml, but there’s no out of the box report and you need to rely on plugins.

bottom line: testng provides out of the box reports, junit only exports it to xml


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.