何时进行单元测试以及何时进行集成测试
我对一般测试不太熟悉,正在开发 Grails 应用程序。
我想编写一个测试,说明“调用此操作时,将返回正确的视图”。我不知道如何决定是否应该进行单元测试或集成测试。任何一个测试都会告诉我我想要什么——我该如何决定?
I am new to testing in general and am working on a Grails application.
I want to write a test that says "when this action is called, the correct view is returned". I don't know how to go about deciding if I should make something like this a unit test or an integration test. Either test would show me what I want - how do I decide?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
集成测试的一个问题是速度。对我来说,集成测试需要 15 秒以上才能启动。在那段时间里,某些事情确实会脱离我们的注意力。
我更喜欢在 2 秒内开始的单元测试,并且可以在 15 秒内运行多次。特别是对于
mockDomain()。
特别是对于 Grails 2.0 在单元测试中实现标准和命名查询。单元测试的另一个论据是它们迫使您解耦代码。集成测试总是诱使您仅仅依赖某些现有的和初始化的其他组件。
One problem with integration tests is their speed. For me, integration tests take 15+ seconds to start up. In that time, certain things do slip out of mind focus.
I prefer to go with unit tests that start in no more then 2 sec and can be run several times in those 15 seconds. Especially with
mockDomain().
Especially with Grails 2.0 implementing criteria and named queries in unit tests.One more argument for unit tests is they force you to decouple your code. Integration tests always tempt you to just rely on some other component existing and initialized.
来自 Grails 文档第 9.1 节
来自 Grails 文档第 9.2 节
这意味着单元测试与 Grails 环境完全隔离,而集成测试则不然。根据 Scott Davis(这篇文章,这样写是可以的仅集成测试...
From Grails Docs section 9.1
From Grails Docs section 9.2
What this means is that a unit test is completely isolated from the Grails environment whereas an integration test is not. According to Scott Davis, author of this article, it is acceptable to write only integration tests...
首先浏览 grails 指南的这一章 http://grails.org/doc /latest/guide/9.%20Testing.html
它讨论了测试控制器以及获得控制器响应的能力,如下所示:
。
现在决定哪个测试更像是一门艺术而不是科学 我更喜欢单元测试,因为它们运行得更快:)
First go through this chapter of the grails guide http://grails.org/doc/latest/guide/9.%20Testing.html
It talks about testing controllers and ability to get controller response like so :
controller.response.contentAsString
Now deciding on which test is more of an art rather than science. I prefer unit tests cause they are faster to run :)
这是一个非常有趣且具有挑战性的问题,但事实是,这实际上取决于您到底要测试什么。
进行以下测试:“将一本书保存到数据库”。提示在描述中。我们说我们需要一本书,我们需要一个数据库,所以在这种情况下单元测试不起作用,因为我们需要集成数据库。
我的建议是写下完整的测试描述,然后像上面那样分解它。它会给您提示来帮助您做出决定。
使用 spock 可以使这变得更容易,您可以使用字符串作为测试名称。
Its a really interesting and challenging question to answer, but the truth is it really depends on what exactly you are testing.
Take the following test: "saving a book to the database". The hints are in the description. We are saying we need a book and we need a database, so in this case a unit test wont do because we need the integrated database.
My advice is write the full test description down and break it down like I did above. It will give you the hints to help you decide.
This is made easier with spock where you can use strings for test names.