In this tutorial, I am going to cover the Introduction to TestNG and its uses in Selenium.
Topics to be covered?
- What is TestNG?
- Advantages of TestNG.
- Annotations of TestNG.
- Difference between JUnit and TestNG.
TestNG is a testing framework which is used to design and maintain test scripts efficiently and generate test execution results in the form of HTML report. It covers a wide range of testing from Unit testing to end-to-end testing.
Advantages -
- It provides effective HTML report that explains test execution result.
- It allows parallel execution of test cases.
- It supports parameterization using @parameters annotation.
- It provides flexibility to prioritize test cases.
- It provides flexibility to ignore the execution of test cases.
- It provides assertions to examine the expected and actual outcome.
- It supports grouping of test cases.
- It supports Data Driven Testing using @DataProvider annotation.
Annotations in TestNG –
- @BeforeSuite – Method under this annotation will execute before the execution of all the tests in the suite.
- @AfterSuite – Method under this annotation will execute after the execution of all the tests in the suite.
- @BeforeClass – Method under this annotation will execute before the execution of first @test annotation.
- @AfterClass – Method under this annotation will execute after the execution of all @test annotation.
- @BeforeMethod – Method under this annotation will execute before the execution of each @test annotation.
- @AfterMethod – Method under this annotation will execute after the execution of each @test annotation.
- @Test – Defines a particular functionality of the application.
- @BeforeTest – Method under this annotation will execute before the execution of first @test annotation.
- @AfterClass – Method under this annotation will execute after the execution of all @test annotation.
JUnit
It is also a testing framework like TestNG. However, TestNG has some more useful features compared to JUnit. So we can say TestNG is the enhanced version of JUnit.
Similarities between JUnit and TestNG
- Annotations - Both have similar annotations(@Test, @BeforeClass, @AfterClass), Almost similar annotations(TestNG has @BeforeMethod and @AfterMethod, similarly JUnit has @Before and @After).
- Annotation to set TimeOut - Both have @Test(timeOut = 1000) in miliseconds.
- Annotation to Ignore Test - TestNG has @Test(enabled=true) and JUnit has @Ignore.
- Annotation for Exception - TestNG has @Test(expectedExpecptions=ExceptionName.class) and JUnit has @Test(expected=ExceptionName.class)
- Annotations - Unlike TestNG, JUnit does not provide @BeforeSuite, @AfterSuite, @BeforeTest and @AfterTest annotations.
- TestNG provides Grouping of test cases whereas JUnit does not.
- TestNG provides parallel execution of test cases whereas JUnit does not.
- Achieving Parametrization in JUnit is more complex than that of TestNG.
- TestNG provides Dependency of a method on other methods functionality of test cases whereas JUnit does not.