何时进行单元测试以及何时进行集成测试

发布于 2024-12-09 05:50:26 字数 122 浏览 0 评论 0原文

我对一般测试不太熟悉,正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

披肩女神 2024-12-16 05:50:26

集成测试的一个问题是速度。对我来说,集成测试需要 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.

巡山小妖精 2024-12-16 05:50:26

来自 Grails 文档第 9.1 节

单元测试是“单元”级别的测试。换句话说,你是
测试单个方法或代码块而不考虑
周边基础设施。在 Grails 中你需要具有特殊性
意识到单元测试和集成测试之间的区别,因为
单元测试 Grails 不会注入任何现有的动态方法
在集成测试和运行时。

来自 Grails 文档第 9.2 节

集成测试与单元测试不同,您拥有完全访问权限
测试中的 Grails 环境。 Grails 将使用
内存中的 HSQLDB 数据库用于集成测试并清除所有
每次测试之间来自数据库的数据。

这意味着单元测试与 Grails 环境完全隔离,而集成测试则不然。根据 Scott Davis这篇文章,这样写是可以的仅集成测试...

单元测试与集成测试

正如我之前提到的,Grails 支持两种基本类型的测试:单元
和整合。两者之间没有语法差异 -
两者都是使用相同的断言编写为 GroovyTestCase。这
区别在于语义。单元测试旨在测试班级
隔离,而集成测试允许您测试类
在完整的运行环境中。

坦率地说,如果您想将所有 Grails 测试编写为
集成测试,这对我来说很好。所有的圣杯
create-* 命令会生成相应的集成测试,因此大多数
人们只是使用已有的东西。正如您将在刚刚看到的
此刻,您想要测试的大部分内容都需要完整的
无论如何,环境都要启动并运行,所以集成测试是一个
相当不错的默认值。如果您有非核心 Grails 类,您需要
喜欢测试,单元测试完全没问题。

From Grails Docs section 9.1

Unit testing are tests at the "unit" level. In other words you are
testing individual methods or blocks of code without considering for
surrounding infrastructure. In Grails you need to be particularity
aware of the difference between unit and integration tests because in
unit tests Grails does not inject any of the dynamic methods present
during integration tests and at runtime.

From Grails Docs section 9.2

Integration tests differ from unit tests in that you have full access
to the Grails environment within the test. Grails will use an
in-memory HSQLDB database for integration tests and clear out all the
data from the database in between each test.

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

Unit vs. integration tests

As I mentioned earlier, Grails supports two basic types of tests: unit
and integration. There's no syntactical difference between the two —
both are written as a GroovyTestCase using the same assertions. The
difference is the semantics. A unit test is meant to test the class in
isolation, whereas the integration test allows you to test the class
in a full, running environment.

Quite frankly, if you want to write all of your Grails tests as
integration tests, that's just fine with me. All of the Grails
create-* commands generate corresponding integration tests, so most
folks simply use what is already there. As you'll see in just a
moment, most of the things you want to test require the full
environment to be up and running anyway, so integration tests are a
pretty good default. If you have noncore Grails classes that you'd
like to test, unit tests are perfectly fine.

☆獨立☆ 2024-12-16 05:50:26

首先浏览 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 :)

橘味果▽酱 2024-12-16 05:50:26

这是一个非常有趣且具有挑战性的问题,但事实是,这实际上取决于您到底要测试什么。

进行以下测试:“将一本书保存到数据库”。提示在描述中。我们说我们需要一本书,我们需要一个数据库,所以在这种情况下单元测试不起作用,因为我们需要集成数据库。

我的建议是写下完整的测试描述,然后像上面那样分解它。它会给您提示来帮助您做出决定。

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文