views:

120

answers:

7

I have read some comparisons of JUnit and TestNG and it looks like TestNG has more configuration options. On the other hand JUnit is more supported by IDEs, building tools, has more plugins.

I have no experience in writing unit tests. Which tool should I prefer?

P.S. I think my question is more like: Should I give TestNG a try, or just stick with JUnit as everybody else?

P.S. We develop web applications, so I think the choice should also consider that we will use Selenium later for functional testing.

A: 

Combination of TestNg and Unitils is unbeatable..It would suffice all your requirements..

Not sure I agree. Doing very similar things in two different ways strikes me as a bad idea. Consistency is important.
GaryF
A: 

I always vote for 'as everybody else', because: this experience in most cases is more valuable, because there is a lot of info, and, for jUnit specifically, it is ported to many languages, so this experience will be 'cross-platform'.

foret
+2  A: 

If this is your first time, I'd recommend JUnit.

It was the first, most popular. It's well documented, has great tool support, and is the metaphor that is translated-- at least initially-- to all the different languages (see xUnit implementations). It should work fine for most any project, and it's a good tool to know. It will be a good baseline for you as a programmer.

There are other features of the "alternatives"-- that's why there are alternatives-- but often it's a matter of style more than anything else. TestNG may have a few different features, but JUnit has also evolved with features like annotations, alternative matchers, etc.

Yes, JUnit will work fine with Selenium when the time comes.

ndp
There is still a lot of legacy examples and tutorials for JUnit 3 around. It would confuse beginner tremendously, so make sure you are using JUnit 4 resources.
grigory
A: 

Junit is most popular it supports plugins so better to use it

Ganesh
A: 

JUnit and TestNG are both quite similar and once you know one of them it is easy to pick up the other.

The main reason I use TestNG is for test groups and test dependencies. You can assign tests to different groups and then easily run all the tests in a group, or exclude certain tests from a group. For example, if I want to isolate a couple of tests, I can simply add them to a test group "fahd" and then run this group only. Test dependencies allow you to skip tests when a dependent test fails. You may think these features are not very important, but once you use them you'll wonder how you ever lived without them.

dogbane
JUnit 4.8 added `@Category` which allows you to assign tests to a category and run all of the tests in a category. See http://stackoverflow.com/questions/2176570/how-to-run-all-tests-belonging-to-a-certain-category-in-junit-4/2176791#2176791 JUnit does not have a concept of dependent tests.
NamshubWriter
A: 

I work as JavaEE devel. We massively use JUnit for testing our business logic modules.It's very powerful, flexible and we use his html report in official release docs as a proof of quality.

savo.fra
+2  A: 

TestNG was written to overcome some perceived limitations of JUnit 3. Check out Cedric's blog post or other articles on the TestNG site to see his thinking.

Note that some of the limitations of JUnit 3 were by design and TestNG deliberately allows you to do things that the designers of JUnit expressly prevented.

The biggest advantage TestNG had over JUnit 3 was that it allowed you to use annotations to define your tests, rather than forcing you to extend the JUnit base classes. JUnit 4 can also use annotations in the same way now, narrowing the gap.

The biggest remaining difference between TestNG and JUnit is that TestNG allows your tests to be dependent on one another, and allows you to express those dependencies through the test annotations. JUnit instead forces all your tests to be independent of one another.

There are other differences too, of course – TestNG defaults to assertEquals(actual, expected, message) whereas JUnit is assertEquals(message, expected, actual) – but this is the biggest.

I'd say: pick one, and try it out. Write some tests. Both work fine with Selenium. Both work fine with ant. Both work fine with CruiseControl or Hudson. Learning to write good unit tests is far more important than learning a particular set of commands (they are pretty similar anyway).

TestNG has more power and fewer restrictions, IMO, but that gives you more opportunities to get things wrong too, of course.

Bill Michell
really good detailed answer, +1.
fastcodejava