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 distinction | JUnit | TestNG |
---|---|---|
Purpose | Unit Testing | unit, functional, end-to-end, integration, etc |
Group Test | No | Yes |
Dependency Test | No | Yes |
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 suite | No | @BeforeSuite |
Execute after all tests in the suite | No | @AfterSuite |
Execute Before a Test Run | No | @BeforeTest |
Execute After a Test Run | No | @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 here | No | @AfterGroups |
Dynamic Test Input | No | Yes with @DataProvider |
Can Run Test of Other Library | No | TestNG can run test of JUNIT |
Dependency Injunction for tests | No | Yes with Google Guice |
Parallel Testing | No | Yes |
Parametrized Test Execution Time | Less | More and Complicated |
Ease of Use | Less | Better than JUNIT |