I am new to automated testing in Java. Which tool

2020-07-18 07:25发布

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.

7条回答
Explosion°爆炸
2楼-- · 2020-07-18 07:49

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.

查看更多
姐就是有狂的资本
3楼-- · 2020-07-18 07:49

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.

查看更多
再贱就再见
4楼-- · 2020-07-18 07:53

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

查看更多
时光不老,我们不散
5楼-- · 2020-07-18 07:55

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

查看更多
Anthone
6楼-- · 2020-07-18 08:05

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.

查看更多
对你真心纯属浪费
7楼-- · 2020-07-18 08:06

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.

查看更多
登录 后发表回答