views:

268

answers:

5
+3  Q: 

JUnit vs TestNG

For one reason or another I've been asked to write a short summary to what the advantages and disadvantages of both JUnit and TestNG are compared to each other. Ideally I want to suggest what types of project each would be used for.

I have essentially no experience with Unit Testing, so I have been reading up on each of the tools, as well on unit testing in general. My problem is that I am having trouble finding significant differences between JUnit and TestNG.

So what I want to do is ask other users who have had more experience with both of these pieces of software. For what reasons did you choose either JUnit or TestNG, and what features in each are most important to you or your team? Finally, what kind of project do you think each is suited for?

+2  A: 

Junit is much more widely used/understood in the industry. This makes it easier for people to move onto your project and be productive.

Paul McKenzie
I have never used TestNG, so I can't really say which is better.
Paul McKenzie
This is what i found aswell, that JUnit is the defacto standard. I'm hoping someone will give me a good reason to use TestNG.
Kris M
Anyone who is familiar with one of the tools should be able to switch to the other and be productive in very little time. The core ideas in TestNG and JUnit 4 are pretty much the same. The differences are mostly in the more advanced features.
Dan Dyer
+2  A: 

JUnit always creates a new instance of the test class for each test. AFAIK (I wasn't able to find it from TestNG's documentation), TestNG reuses the same test class instance for many tests.

Always creating a new test class instance for each test is important because of test isolation and repeatability. Then you can safely store into test class fields those objects which are used from many tests, without fear of their mutated state being leaked and causing nondeterministic and hard-to-find test failures. You can even make the fields final and don't need a setup method.

Esko Luontola
TestNG reuses the same instance for your tests. Test isolation is important for unit tests but a big hurdle when you write integration tests. A lot of these depend on expensive state that need to be maintained between calls (e.g. why display a web page if the login page test failed earlier?).To make things worse, JUnit users very often find the need to maintain some state across method calls and they end up doing so with static fields, which lead to subtle bugs and fragile code.
Cedric Beust
By the way, if you really need to make sure that no state will leak from one test to the other, it's much safer to initialize this state in @BeforeClass or @BeforeMethod than relying on the framework to do this for you (you can't really control when these instances are created, if they are pooled, etc...).
Cedric Beust
+4  A: 

I've mostly been using JUnit, but I've used TestNG a bit in one project. I still prefer JUnit over TestNG and I'll list the reasons below.

  • JUnit is more commonly used, thus finding documentation for a JUnit problem is usually easier than with TestNG.

  • Eclipse's JUnit plugin is a lot better. I like to run a lot adhoc tests by just clicking on a package/class or several packages/classes and hitting Run As -> JUnit Test. I wasn't able to do this with the TestNG plugin. If I remember correctly it was either one class, one package or one test method. I would have had to construct custom groups to be able to run these tests and that is too much of a chore.

  • TestNG does offer some functionality that JUnit doesn't. For instance @BeforeGroups and @AfterGroups seem like a good idea if you are doing a lot of integration tests. With JUnit I go around this by using the dependency injection framework to construct and destroy the test data if necessary, so I haven't really needed this feature.

ponzao
Thanks for detailing the specific features you liked, this is most useful.
Kris M
A: 

Advantages of writing test cases using TestNG is: You don't have to extend any class or follow method naming conventions. All that you need to do is use @Test annotation.

With TestNG you can pass parameters to methods using DataProviders or testng.xml file which will be helpful in testing a single method with different inputs.

Groups in TestNG are very useful in organizing test cases as well as running a specific set of groups(include/exclude groups). ex: tests which are OS specific.

With Method Dependencies and Group Dependencies, if a method/group fails all the methods which depend on that group/method are skipped which saves both time and resources.

laxman
A: 

TestNG has more features, some of them are highlighted in this article.

ANother cool TestNG feature: method interceptors.

Pierre Gardin