In this tutorial we will discuss on the difference Between TestNG and JUnit.
- TestNG supports Parallel Testing. But JUnit does not support this feature.
- It supports the dependency Testing with the help of dependsOnMethod or dependOnGroup. JUnit does not support this feature.
- JUnit is not able to support special annotations. But TestNG can support.
- They are @AfterGroups, @AfterSuite, @AfterTest, @BeforeTest, @BeforeSuite, @BeforeGroups.
- TestNG possesses not only the major features but also a lot of functions in terms of selenium WebDriver testing framework.
- TestNG reuses the same class for all Test case method. But JUnit does not.
- TestNG supports Grouping of test. But JUnit does not.
- The Annotation for ignoring the Testing in case of TestNG is @Test( enable = false). But in case of JUnit it is @ignore.
- Annotation for Executes after each test method in case of TestNG is @AfterMethod. But in case of JUnit it is @After.
- Annotation for Executes before each test method in case of TestNG is @BeforeMethod. But in case of JUnit it is @Before.
Below is the example of annotations difference.
Difference Between TestNG and JUnit
|Executes before each test method||@BeforeMethod||@Before|
|Executes after each test method||@AfterMethod||@After|
|Annotation to ignore a test||@Test(enable=false)||@ignore|
|Annotation for exception||@Test(expectedExceptions = ArithmeticException.class)||@Test(expected = ArithmeticException.class)|
|Execution of before all tests in the suite||@BeforeSuite||N/A|
|Execution of after all tests in the suite||@AfterSuite||N/A|
|Executes before a test runs||@BeforeTest||N/A|
|Executes after a test runs||@AfterTest||N/A|
|I will execute before first test method which belong to any of the groups is invoked||@BeforeGroups||N/A|
|I will execute after the last test method which belong to any of the groups is invoked||@AfterGroups||N/A|
From the above discussion on the differences between TestNG and JUnit, we understood that TestNG has some advanced features over JUnit. Though JUnit is older, some professional also use Junit in their work.
The main advantage of the TestNG over the JUnit is the annotation. Annotation of the TestNG is more simpler than Junit. This makes the test script easy to understand and ease to maintain.
Some advance features like Parameterization, Group Test, Dependency Test is not supported by JUnit. But TestNG supports all the advance features mentioned above.
dependsOnMethod and dependOnGroup are not supported by JUnit. But through TestNG we can achieve these 2 important features.