Tuesday, December 12, 2017

Why we use TestNG instead of JUnit?

Why we use TestNG instead of JUnit?

  1. The declaration of @BeforeClass and @AfterClass method has to be static in JUnit whereas, there is more flexibility in TestNG in the method declaration, it does not have these constraints.
  2. In TestNG, we can parametrize tests using 2 ways. @Parameter or @DataProvider annotation.
    i) @Parameter for simple cases, where key value mapping is required.(data is provided through xml file)
    ii) @DataProvider for complex cases. Using 2 dimensional array, It can provide data.
  3. In TestNG, since @DataProvider method need not be static, we can use multiple data provider methods in the same test class.
  4. Dependency Testing: In TestNG, if the initial test fails, then all subsequent dependent tests will be skipped, not marked as failed. But JUnit marked it failed.
  5. Grouping: Single tests can belong to multiple groups and then run in different contexts (like slow or fast tests). A similar feature exists in JUnit Categories but lacks the @BeforeGroups / @AfterGroups TestNG annotations that allow initializing the test / tearing it down.
  6. 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.
  7. TestNG @DataProvider can also support XML for feeding in data, CSVs, or even plain text files.
  8. TestNG allows you to declare dependencies between tests, and skip them if the dependency test didn’t pass.
@Test(dependsOnMethods = { "dependOnSomething" })
This functionality doesn’t exist in JUnit
  1. Reporting:
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.

Resource Link:

  1. A Quick JUnit vs TestNG Comparison
  2. JUnit vs. TestNG: Which Testing Framework Should You Choose?
A good difference is given in this tutorial side by side: TestNG Vs JUnit: What's the Difference?

No comments:

Post a Comment