What is the difference between TestNG and jUnit?

544    Asked by AndrewJenkins in QA Testing , Asked on May 9, 2022

 I have started Selenium scripting in one of my projects and I want to know the exact difference between Junit & TestNG. Which one is useful for Testers in Selenium? Any reference link/Site for the same?

Answered by Anil Jha

The difference between TestNG and jUnit is -


TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc... Additional 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.
Facilitates user with effective means of Report Generation using ReportNG
Annotations:
JUnit: @BeforeClass and @AfterClass methods have to be declared as static. TestNG does not have this constraint.
TestNG: It has provided four additional setup/teardown pairs for the suite, test and groups, i.e. @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeGroups and @AfterGroups, @BeforeMethod, @BeforeClass, @AfterClass and @AfterMethod.
Parameterized test:

This feature is implemented in both tools, however in quite different ways.

TestNG has basically two ways for providing varying parameter values to a test method: by setting the testng.xml, and by defining a @DataProvider method.
In JUnit, @RunWith and @Parameters are used together to facilitate parameterized tests, while the @Parameters method has to return List[] with all the actual values, which will be fed into a dedicated class constructor as an argument.
Why choose TestNG?

I've talked about the features that differentiate TestNG for me, but there are a few more that aren't yet available in JUnit. For example, TestNG uses test groups, which can categorise tests according to features such as run times. As I said at the beginning of this column, JUnit 4 and TestNG are similar on the surface. But whereas JUnit is designed to hone in on a unit of code, TestNG is meant for high-level testing. Its flexibility is especially useful with large test suites, where one test's failure shouldn't mean having to rerun a suite of thousands.

Each framework has its strengths, and there's nothing stopping you from using both in concert.



Your Answer

Interviews

Parent Categories