JUnit In Action读书笔记(7) -- Managing resources with
关键字: managing resources with a fixture2.6.1 Managing resources with a fixture
有时,一些tests是需要外部资源的,而这些资源的配置却很麻烦.这方面很典型的例子就是数据库的连接.而有时为了一个测试,我们得需要很复杂的data stuctures或一长串的随机输入.
要是把这些很典型的setup代码也写进tests里的话,那可就显得有些傻了.毕竟我们要测试的不是搭建资源的能力--我们仅仅是需要一个稳定的测试环境.这里的测试环境set在JUnit里称为"test fixture".
DEFINITION: ;fixture--The set of common resources or data that you need to run one or more tests.
JUnit framework可以通过TestCase的setUp和tearDown方法来自动地创建和销毁.在运行每一个tests前,TestCase会自动调用setUp方法,而在每一个test完成之后TestCase也会自动调用tearDown.写测试代码时,我们之所以将很多相关的test method写入the same TestCase里的重要的原因是想share the fixture code.
在实际中,现在many developers现在用mock objects或stubs来模拟数据库连接和其它更为复杂的资源的配置.
数据库连接问题的例子就是一个很好的例子来说明我们为什么需要a fixture.若一个TestCase同时又包含了很多个数据库测试的话,这些连接都是需要一个对数据库的全新连接,利用JUnit中的fixture我们就可以很轻易地为每一个test都创建一个全新的连接,而无需replicating code.我们也可以很轻松地利用fixture创建新的input files.doing this means you do not have to carry your test files with your tests, and you always have a known state before the test is executed.
我们也可以通过JUnit里Assert接口提供的一些utility methods来重用代码,下一节我们解释这个utility methods.
JUnit design goals
When it is easy to reuse a fixture between tests, you can write tests more quickly. Each time you reuse the fixture, you decrease the initial investment made when the fixture was created. The TestCase fixtures speak to JUnit’s third design goal:
The framework must lower the cost of writing tests by reusing code.
2.6.2 Creating unit test methods
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 29437 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
链接
最新评论
-
对浏览器中的context menu ...
Drag dropper
-- by whqida -
Spring中的load-time weav ...
没啥,有些class不在spring的管理之中,通过 load-time wea ...
-- by ray_linn -
对浏览器中的context menu ...
别搞笑了, 这是浏览器的 Context Menu? 这只不过是响应了鼠标右击事 ...
-- by fcoffee -
IE tab在Firefox里开发时 ...
firebug只能用于ff, 并不能用于ff下的ietab, 原因显而易见.如果 ...
-- by fcoffee -
IE tab在Firefox里开发时 ...
还网告知 该如何操作呢 我还是要到ie下再调试啊
-- by jianfeng008cn






评论排行榜