7

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

4
  • JUnit and TestNG are quite similar, the main difference though, is that TestNG supports Dependency Testing while JUnit does not.
    – sqa
    Aug 4, 2016 at 2:13
  • 1
    Now there's JUnit5 which has more feature than JUnit4, including test groups.
    – dzieciou
    Dec 22, 2016 at 12:43
  • @sqa What is dependency testing? Do you mean I can test Maven dependencies?
    – dzieciou
    Dec 22, 2016 at 18:39
  • @dzieciou Dependency testing in is simply relying on other data from other test methods by implementing a strict order on how testcases are executed and along the way you can share data from one test function to another.
    – sqa
    Dec 29, 2016 at 2:39

2 Answers 2

10

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 categorize 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.

enter image description here

4
  • Re: TestNG's goal is much wider, which includes not only unit testing, but also support of integration and acceptance testing, etc. Which features of TestNG support integration and acceptance testing?
    – dzieciou
    Dec 22, 2016 at 12:25
  • I downvoted because I miss explanation of what makes TestNG better for integration testing.
    – dzieciou
    Dec 22, 2016 at 19:16
  • 1
    Your post implicitly suggests that JUnit cannot be used for integration and end-to-end testing. However, there's no obstacle to use JUnit for integration and end-to-end testing. For instance, here's a JUnit example of testing integration of a system with Cassandra: github.com/jsevellec/cassandra-unit/wiki/…. And here's an example of Allure reporting library combined with JUnit: github.com/allure-examples/allure-junit-example. Allure is used often used in end-to-end scenarios.
    – dzieciou
    Dec 23, 2016 at 9:25
  • Finally, there are tons of examples of using JUnit in combination with Selenium, e.g., stackoverflow.com/questions/10577254/…. Selenium is obviously used in end-to-end tests.
    – dzieciou
    Dec 23, 2016 at 9:28
2

Major difference in TestNG and JUnit test frameworks is their scope.

I suggest to use TestNG as core unit test framework for Java project, because TestNG is more advance in parameterize testing, dependency testing and suite testing (Grouping concept). 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.

Refer this link to get details about difference between JUnit and TestNG.

2
  • Re: TestNG addresses testing at a higher level. Which features of TestNG address testing at a higher level?
    – dzieciou
    Dec 22, 2016 at 12:29
  • 1
    High Level Features : Parameterized test(Object as parameter), Dependency Test(Dependency Test will execute before a desired method), Group Test(Allow to group multiple testcases).
    – Ravi Ojha
    Jan 24, 2017 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.