Since Selenium WebDriver is widely used in Automation, it became important for the Selenium WebDriver to be used in a more productive way. There are Frameworks available that can be used along with the WebDriver like JUnit, TestNG, which organizes the Test scripts in a better way.
Junit
JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks.
Test methods must be annotated by the @Test annotation. If the situation requires, it is also possible to define a method to execute before (or after) each (or all) of the test methods with the @Before (or @After) and @BeforeClass (or @AfterClass) annotations.
Features
· JUnt is an open source framework which is used for writing & running tests.
· Provides Annotation to identify the test methods.
· Provides Assertions for testing expected results.
· Provides Test runners for running tests.
· JUnit tests allow you to write code faster which increasing quality
· JUnit is elegantly simple. It is less complex & takes less time.
· JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.
· JUnit tests can be organized into test suites containing test cases and even other test suites.
· Junit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.
Though Junit has all these features, it does have few limitations which make coding complex and not suitable for Large Test Suites. And these limitations can be overcome by TestNG.
TestNG
TestNG is a testing framework that overcomes the limitations of another popular testing framework called JUnit. The "NG" means "Next Generation". Most Selenium users use this more than JUnit because of its advantages. There are so many features of TestNG, but we will only focus on the most important ones that we can use in Selenium.
Features
· Annotations.
· Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc...).
· Test that your code is multithread safe.
· Flexible test configuration.
· Support for data-driven testing (with @DataProvider).
· Support for parameters.
· Powerful execution model (no more TestSuite).
· Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
· Embeds BeanShell for further flexibility.
· Default JDK functions for runtime and logging (no dependencies).
· Dependent methods for application server testing.
TestNG is designed to cover all categories of tests:
· Unit,
· Functional,
· End-to-End,
· Integration and etc…
TestNG can generate reports based on our Selenium test results.
· WebDriver has no native mechanism for generating reports.
· TestNG can generate the report in a readable format like the one shown below.
TestNG simplifies the way the tests are coded
· There is no more need for a static main method in our tests. The sequence of actions is regulated by easy-to-understand annotations that do not require methods to be static.
· Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely. These exceptions are reported as failed steps in the report.
Advantages of TestNG over Junit
· In Junit we have to declare @BeforeClass and @AfterClass which is a constraint where as in TestNG there is no constraint like this.
· Additional Levels of setUp/tearDown level are available in TestNG like @Before/AfterSuite, @Before/AfterTest and @Before/AfterGroup
· No Need to extend any class in TestNG.
· There is no method name constraint in TestNG as in Junit. You can give any name to the test methods in TestNG.
· In TestNG we can tell the test that one method is dependent on another method where as in Junit this is not possible. In Junit each test is independent of another test.
· Grouping of test cases is available in TestNG whereas the same is not available in Junit.
Execution can be done based on Groups. For ex. If you have defined many cases and segregated them by defining 2 groups as Sanity and Regression. Then if you only want to execute the “Sanity” cases then just tell TestNG to execute the “Sanity” and TestNG will automatically execute the cases belonging to the “Sanity” group.
Conclusion
If it is some basic Unit Testing, both the frameworks are basically the same. Both the frameworks allow you to test the code in a quick and effective manner. They have had tool support in Eclipse and other IDE. They have also had support in the build frameworks like Ant and Maven. For starters JUnit has always been the choice because it was the first framework for Unit Testing and has always been available.
Junit Test Suites and projects can be migrated to TestNG. The easiest way to convert your JUnit tests to TestNG is to use the Eclipse TestNG plug-in refactoring support.
TestNG is meant for high-level testing and complex integration test. Its flexibility is especially useful with large test suites. In addition, TestNG also cover the entire core JUnit4 functionality. It’s just no reason for me to use JUnit anymore.