Difference between TestNG and JUNIT?

Profile picture for user devraj
Submitted by devraj on

Despite being easy to use and straightforward, JUnit has its own limitations which give rise to the need of bringing TestNG into the picture. TestNG is an open source framework which is distributed under the Apache Software License and is readily available for download.

Both have @Test annotation, @BeforeClass to execute before the first test method is invoked in current class. @AfterClass Execute after all the Test methods in the current class. Timeout @Test(timeout=1000), When a test method is taking longer time than the specified time (in milliseconds) to execute, then that test method will be terminated and marked as failed.

There are various advantages that make TestNG superior to JUnit. Some of them are mentioned in below difference:

Point of distinctionJUnitTestNG
PurposeUnit Testingunit, functional, end-to-end, integration, etc
Group TestNoYes
Dependency TestNoYes
Execute before each test method@Before@BeforeMethod
Execute after each test method@After@AfterMethod
Annotation to ignore a test@ignore@Test(enabled=false)
Annotation for Exception@Test(expected = ArithmeticException.class)@Test(expectedExceptions = ArithmeticException.class)
Executes before all tests in the suiteNo@BeforeSuite
Execute after all tests in the suiteNo@AfterSuite
Execute Before a Test RunNo@BeforeTest
Execute After a Test RunNo@AfterTest
Executes before the first test method is invoked that belongs to any of these groups is invoked    No@BeforeGroups
run after the last test method that belongs to any of the groups hereNo@AfterGroups
Dynamic Test InputNoYes with @DataProvider
Can Run Test of Other LibraryNoTestNG can run test of JUNIT
Dependency Injunction for testsNoYes with Google Guice
Parallel TestingNoYes
Parametrized Test Execution TimeLessMore and Complicated
Ease of UseLessBetter than JUNIT