Java JUnit 与 TestNG
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit vs TestNG
提问by Sam Merrell
At work we are currently still using JUnit 3 to run our tests. We have been considering switching over to JUnit 4 for newtests being written but I have been keeping an eye on TestNG for a while now. What experiences have you all had with either JUnit 4 or TestNG, and which seems to work better for very large numbers of tests? Having flexibility in writing tests is also important to us since our functional tests cover a wide aspect and need to be written in a variety of ways to get results.
在工作中,我们目前仍在使用 JUnit 3 来运行我们的测试。我们一直在考虑切换到 JUnit 4 来编写新的测试,但我一直关注 TestNG 有一段时间了。你们有哪些使用 JUnit 4 或 TestNG 的经验,并且对于非常大量的测试似乎更有效?编写测试的灵活性对我们来说也很重要,因为我们的功能测试涵盖了广泛的方面,需要以多种方式编写才能获得结果。
Old tests will not be re-written as they do their job just fine. What I would like to see in new tests though is flexibility in the way the test can be written, natural assertions, grouping, and easily distributed test executions.
旧的测试不会被重写,因为它们可以很好地完成他们的工作。不过,我希望在新测试中看到的是测试编写方式的灵活性、自然断言、分组和易于分布式的测试执行。
采纳答案by Mike Stone
I've used both, but I have to agree with Justin Standard that you shouldn't really consider rewriting your existing tests to any new format. Regardless of the decision, it is pretty trivial to run both. TestNG strives to be much more configurable than JUnit, but in the end they both work equally well.
我两种都用过,但我必须同意 Justin Standard 的观点,即您不应该真正考虑将现有测试重写为任何新格式。无论决定如何,运行两者都非常简单。TestNG 力求比 JUnit 更具可配置性,但最终它们都同样出色地工作。
TestNG has a neat feature where you can mark tests as a particular group, and then easily run all tests of a specific group, or exclude tests of a particular group. Thus you can mark tests that run slowly as in the "slow" group and then ignore them when you want quick results. A suggestion from their documentation is to mark some subset as "checkin" tests which should be run whenever you check new files in. I never saw such a feature in JUnit, but then again, if you don't have it, you don't REALLY miss it.
TestNG 有一个简洁的功能,您可以将测试标记为特定组,然后轻松运行特定组的所有测试,或排除特定组的测试。因此,您可以将运行缓慢的测试标记为“慢”组,然后在需要快速结果时忽略它们。他们的文档中的一个建议是将一些子集标记为“签入”测试,每当您签入新文件时都应该运行这些测试。我从未在 JUnit 中看到过这样的功能,但是再说一次,如果您没有它,您就没有真的很想念它。
For all its claims of high configuration, I did run into a corner case the a couple weeks ago where I couldn't do what I wanted to do... I wish I could remember what it is, but I wanted to bring it up so you know that it's not perfect.
尽管它声称具有高配置,但几周前我确实遇到了一个角落案例,我无法做我想做的事情......我希望我能记住它是什么,但我想提出它所以你知道它并不完美。
The biggest advantage TestNG has is annotations... which JUnit added in version 4 anyways.
TestNG 的最大优势是注释……无论如何,JUnit 在版本 4 中添加了注释。
回答by Justin Standard
First I would say, don't rewrite all your tests just to suit the latest fad. Junit3 works perfectly well, and the introduction of annotations in 4 doesn't buy you very much (in my opinion). It is much more important that you guys writetests, and it sounds like you do.
首先我想说,不要为了适应最新的时尚而重写所有的测试。Junit3 工作得非常好,4 中引入注解并没有给你带来太多好处(在我看来)。更重要的是你们编写测试,这听起来像你做的。
Use whatever seems most natural and helps you get your work done.
使用任何看起来最自然的方式并帮助您完成工作。
I can't comment on TestNG b/c I haven't used it. But I would recommend unitils, a great wrapper for JUnit/TestNG/DBUnit/EasyMock, regardless of which route you take. (It supports all the flavors mentioned above)
我无法评论 TestNG b/c 我没有使用过它。但我会推荐unitils,它是 JUnit/TestNG/DBUnit/EasyMock 的一个很好的包装器,无论你走哪条路。(它支持上面提到的所有口味)
回答by abyx
About a year ago, we had the same problem. I spent sometime considering which move was better, and eventually we realized that TestNG has no 'killer features'. It's nice, and has some features JUnit 4 doesn't have, but we don't need them.
We didn't want people to feel uncomfortable writing tests while getting to know TestNG because we wanted them to keep writing a lot of tests.
Also, JUnit is pretty much the de-facto standard in the Java world. There's no decent tool that doesn't support it from the box, you can find a lot of help on the web and they added a lot of new features in the past year which shows it's alive.
大约一年前,我们遇到了同样的问题。我花了一些时间考虑哪个动作更好,最终我们意识到 TestNG 没有“杀手级功能”。它很好,并且具有 JUnit 4 没有的一些功能,但我们不需要它们。
我们不希望人们在了解 TestNG 的同时对编写测试感到不舒服,因为我们希望他们继续编写大量测试。
此外,JUnit 几乎是 Java 世界中事实上的标准。没有不支持它的像样工具,您可以在网上找到很多帮助,他们在过去一年中添加了许多新功能,这表明它仍然有效。
We decided to stick with JUnit and never looked back.
我们决定坚持使用 JUnit 并且永不回头。
回答by Mark Derricutt
TestNG's biggest draw cards for me include its support test groups, and more importantly - test group dependencies (marking a test as being dependent of a group causes the tests to simply skip running when the dependent group fails).
TestNG 对我来说最大的抽奖卡包括它的支持测试组,更重要的是 - 测试组依赖关系(将测试标记为依赖组会导致测试在依赖组失败时简单地跳过运行)。
TestNG's other big draw cards for me include test parameters, data providers, annotation transformers, and more than anything - the vibrant and responsive user community.
TestNG 对我来说的其他大抽奖包括测试参数、数据提供者、注释转换器,以及最重要的 - 充满活力和响应迅速的用户社区。
Whilst on the surface one might not think all of TestNGs features above might not be needed, once you start to understand the flexibility bring to your tests, you'll wonder how you coped with JUnit.
虽然从表面上看,您可能并不认为上述所有 TestNG 功能都不需要,但一旦您开始了解为您的测试带来的灵活性,您就会想知道您是如何处理 JUnit 的。
(disclaimer - I've not used JUnit 4.x at all, so am unable to really comment on advances or new features there).
(免责声明 - 我根本没有使用过 JUnit 4.x,所以我无法真正评论那里的进步或新功能)。
回答by Mark Derricutt
A couple of additions to Mike Stone's reply:
Mike Stone 的回复补充了几点:
1) The most frequent thing I use TestNG's groups for is when I want to run a single test method in a test suite. I simply add this test to the group "phil" and then run this group. When I was using JUnit 3, I would comment out the entries for all methods but the one I wanted to run in the "suite" method, but then would commonly forget to uncomment them before checkin. With the groups, I no longer have this problem.
1) 我最常使用 TestNG 的组是当我想在测试套件中运行单个测试方法时。我只是将此测试添加到组“phil”,然后运行该组。当我使用 JUnit 3 时,我会注释掉所有方法的条目,但我想在“套件”方法中运行的那个,但通常会忘记在签入之前取消注释。有了小组,我不再有这个问题。
2) Depending on the complexity of the tests, migrating tests from JUnit3 to TestNG can be done somewhat automatically with sed and creating a base class to replace TestCase that static imports all of the TestNG assert methods.
2) 根据测试的复杂性,将测试从 JUnit3 迁移到 TestNG 可以通过 sed 自动完成,并创建一个基类来替换静态导入所有 TestNG 断言方法的 TestCase。
I have info on my migration from JUnit to TestNG hereand here.
回答by Denis Bazhenov
Also one more advantage of TestNG is supporting of parallel testing. In our era of multicores it's important, i think.
TestNG 的另一个优点是支持并行测试。我认为,在我们的多核时代,这很重要。
I also used both frameworks. But i using hamcrest for assertations. Hamcrest allows you easily write your own assert method. So instead of
我也使用了这两个框架。但我使用 hamcrest 进行断言。Hamcrest 允许您轻松编写自己的断言方法。所以代替
assertEquals(operation.getStatus(), Operation.Status.Active);
You can write
你可以写
assertThat(operation, isActive());
That gives you opportunity to use higher level of abstraction in your tests. And this makes your tests more robust.
这让您有机会在测试中使用更高级别的抽象。这使您的测试更加稳健。
回答by ravinikam
I wanted to share the one I encountered today. I found built-in Parameterized runner is quite crude in Junit4 as compare to TestNG (I know each framework has its strengths but still). The Junit4 annotation @parameters is restricted to one set of parameters. I encountered this problem while testing the valid and invalid behavior for functionality in same test class. So the first public, static annotated method that it finds will be used, but it may find them in any order. This causes us to write different classes unnecessarily. However TestNG provides clean way to provide different kind of data providers for each and every method. So we can test the same unit of code with valid and invalid way in same test class putting the valid/invalid data separately. I will go with TestNG.
我想分享我今天遇到的一个。我发现与 TestNG 相比,Junit4 中的内置参数化运行器相当粗糙(我知道每个框架都有其优势,但仍然如此)。Junit4 注释@parameters 仅限于一组参数。我在同一测试类中测试功能的有效和无效行为时遇到了这个问题。所以它找到的第一个公共的、静态的带注释的方法将被使用,但它可以按任何顺序找到它们。这导致我们不必要地编写不同的类。然而,TestNG 提供了一种干净的方式来为每种方法提供不同类型的数据提供者。所以我们可以在同一个测试类中以有效和无效的方式测试相同的代码单元,将有效/无效数据分开。我会和 TestNG 一起去。
回答by mezmo
Cheers to all the above. Some other things I've personally found I like more in TestNG are:
为以上所有干杯。我个人发现我更喜欢 TestNG 的其他一些东西是:
The
@BeforeClass
for TestNG takes place after class creation, so you aren't constrained by only being able to call static methods of your class in it.Parallel and parameterized tests, maybe I just don't have enough of a life... but I just get a kick writing one set of Selenium tests, accepting a driver name as a parameter. Then defining 3 parallel test groups, 1 each for the IE, FF and Chrome drivers, and watching the race! I originally did 4, but way too many of the pages I've worked on break the
HtmlUnit
driver for one reason or another.
在
@BeforeClass
创建类之后TestNG的发生,这样你就不会只能够打电话给你的类的静态方法,它的约束。并行和参数化测试,也许我只是没有足够的生命......但我只是在编写一组 Selenium 测试,接受驱动程序名称作为参数。然后定义 3 个平行测试组,IE、FF 和 Chrome 驱动程序各 1 个,并观看比赛!我最初做了 4 个,但是
HtmlUnit
由于某种原因,我处理的太多页面破坏了驱动程序。
Yeah, probably need to find that life. ;)
是的,可能需要找到那个生活。;)
回答by MostWanted
Your question seems two folded to me. On one had you would like to compare two test frameworks, on the other hand you would like to implement tests easily, have natural assertions, etc...
你的问题对我来说似乎是两回事。一方面您想比较两个测试框架,另一方面您想轻松实现测试,拥有自然断言等......
Ok, firstly JUnit has been playing catchup with TestNG in terms of functionality, they have bridged the gap some what with v4, but not well enough in my opinion. Things like annotations and dataproviders are still much better in TestNG. Also they are more flexible in terms of test execution, since TestNG has test dependency, grouping and ordering.
好的,首先 JUnit 在功能方面一直在追赶 TestNG,他们在 v4 中弥补了一些差距,但在我看来还不够好。诸如注释和数据提供者之类的东西在 TestNG 中仍然要好得多。此外,它们在测试执行方面更加灵活,因为 TestNG 具有测试依赖性、分组和排序。
JUnit still requires certain before/after methods to be static, which limits what you can do prior to the running of tests, TestNG never has this issue.
JUnit 仍然要求某些 before/after 方法是静态的,这限制了您在运行测试之前可以做的事情,TestNG 从来没有这个问题。
TBH, mostly the differences between the two frameworks don't mean much, unless your focusing on integration/automation testing. JUnit from my experience is built from the ground up for unit testing and is now being pushed towards higher levels of testing, which IMO makes it the wrong tool for the job. TestNG does well at unit testing and due to its robust dataproviding and great test execution abilities, works even better at integration/automation test level.
TBH,主要是两个框架之间的差异没有多大意义,除非您专注于集成/自动化测试。根据我的经验,JUnit 是为单元测试而从头开始构建的,现在正被推向更高级别的测试,IMO 使其成为该工作的错误工具。TestNG 在单元测试方面做得很好,并且由于其强大的数据提供和强大的测试执行能力,在集成/自动化测试级别上工作得更好。
Now for what I believe is a separate issue, how to write well structured, readable and maintainable tests. Most of this I am sure you know, but things like Factory Pattern, Command Patternand PageObjects(if your testing websites) are vital, it is very important to have a layer of abstraction between what your testing (SUT) and what the actual test is (assertions of business logic). In order to have much nicer assertions, you can use Hamcrest. Make use of javas inheritance/interfaces to reduce repetition and enforce commonality.
现在我认为是一个单独的问题,如何编写结构良好、可读和可维护的测试。我相信您知道其中的大部分内容,但是工厂模式、命令模式和PageObjects(如果您的测试网站)之类的东西至关重要,在您的测试 (SUT) 和实际测试之间有一个抽象层非常重要是(业务逻辑的断言)。为了获得更好的断言,您可以使用Hamcrest。利用 javas 继承/接口来减少重复并加强通用性。
Almost forgot, also use the Test Data Builder Pattern, this coupled with TestNG's dataprovider annotation is very useful.
差点忘了,还用了Test Data Builder Pattern,这个再加上 TestNG 的 dataprovider annotation 很有用。
回答by djangofan
My opinion about what makes TestNG truly far more powerful:
我对什么使 TestNG 真正强大得多的看法:
1. JUnit still requires the before/after class methods to be static, which limits
what you can do prior to the running of tests, TestNG never has this issue.
2. TestNG @Configuration methods can all take an optional argument to their
annotated methods in the form of a ITestResult, XmlTest, Method, or
ITestContext. This allows you to pass things around that JUnit wouldn't
provide you. JUnit only does this in listeners and it is limited in use.
3. TestNG comes with some pre-made report generation classes that you can copy
and edit and make into your own beautiful test output with very little
effort. Just copy the report class into your project and add a listener
to run it. Also, ReportNG is available.
4. TestNG has a handful of nice listeners that you can hook onto so you can do
additional AOP style magic at certain phases during testing.