Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am new to automated testing in Java. Which tool should I prefer? JUnit or TestNG?

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.

like image 964
Igor Mukhin Avatar asked Dec 22 '22 23:12

Igor Mukhin


2 Answers

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.

like image 31
ndp Avatar answered May 18 '23 21:05

ndp


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.

like image 147
Bill Michell Avatar answered May 18 '23 21:05

Bill Michell