`
xitong
  • 浏览: 6196502 次
文章分类
社区版块
存档分类
最新评论

Dbunit最佳实践

 
阅读更多
原文:http://dbunit.sourceforge.net/bestpractices.html

Best Practices

  1. Use one database instance per developer
  2. Good setup don't need cleanup!
  3. Use multiple small datasets
  4. Perform setup of stale data once for entire test class or test suite
  5. Connection management strategies

Use one database instance per developer

每个开发人员使用一个数据库

Testing can be simplified if you can get your database in a known state before a test is run. A database should only be used for one test at a time; otherwise the database state cannot be guarantied.
让你的数据库在测试运行之前处于一个已知状态可以简化测试.一个数据库在同一时间应该只用于一个测试,否则数据库状态无法保障.

So multiple developers working on the same project should have their own database instance to prevent data corruption. This also simplifies database cleanup, as you don't necessarily need needs to revert it to its initial state.
所以同一项目的多个开发人员应该有每人一个数据库,这样可以防止数据紊乱,这也可以简化数据清除,你不必在每次测试前将数据库回滚到其初始状态.

Good setup don't need cleanup!

好的setup无需清除数据

You should always avoid creating tests that depends on results of preceding tests; thankfully this is the main purpose of DbUnit.
你应该始终避免产生依赖以前测试结果的测试,幸好这也是dbunit主要目标

Don't be afraid to leave your trace after a test; principally if you are using one database instance per developer. If you always put your database in a known state before a test execution, you usually don't need to clean it up.. This simplifies your tests maintenance and reduces the overhead taken by the cleanup procedure. And sometimes, this is very helpful to manually verify the database state after executing a test that fails.
原则上如果你使用"每个开发人员一个数据"的实践,不要害怕在测试之后留下你的数据.如果你在测试运行之前将数据库置于一个已知状态,那么你无需清除数据.这可以简化测试维护和减少清除操作带来的开销.有时候,如果测试失败,这有助于你手工检测数据库.

Use multiple small datasets

使用多个小数据集

Most of your tests do not require the entire database to be re-initialized. So, instead of putting your entire database data in one large dataset, try to break it into many smaller chunks.
你的大部分测试不需要整个数据库每次测试都重新初始化.所以,在一个大型数据库的应用中,无需为整个数据库准备数据集,你应该将整个数据库的数据集拆成一块块的小数据集

These chunks could roughly corresponding to logical units, or components. This reduces the overhead caused by initializing your database for each test. This also facilitates team development since many developers working on different components can modify datasets independently.
这些小块的数据集能大致对应你的逻辑单元或者说组件. 这减少了每次测试都初始化数据库的开销.这对小组开发也极为有利,因为工作于不同组件的开发者可以独立的修改数据集

For integrated testing, you can still use the CompositeDataSet class to logically combine multiple datasets into a large one at run time.
对集成测试来说,你仍然可以使用CompositeDataSet类在运行时候将多个小数据集绑定成一个大的

Perform setup of stale data once for entire test class or test suite

为整个测试类或数据集只setup不变数据一次

If several tests are using the same read-only data, this data could be initialized once for an entire test class or test suite. You need to be cautious and ensure you never modify this data. This can reduce the time required to run your tests but also introduces more risk.
如果多个测试使用相同的只读数据,那么整个测试类或测试集可以只初始化这些数据一次.你必须小心确保你从不会修改这些数据.这也能减少运行测试的时间,但也引入更多风险

Connection management strategies

连接管理策略

Here are the recommended connection management strategies depending whether you test from a remote client or an in-container strategy:

以下是推荐的连接管理策略,分为两类:远程测试和容器内测试

Remote client with DatabaseTestCase

使用DatabaseTestCase的远程客户

You should try to reuse the same connection for the entire test suite to reduce the overhead of creating a new connection for each test. Since version 1.1, DatabaseTestCase is closing every connection in setUp() and tearDown(). Override the closeConnection() method with an empty body to modify this behavior.
你应该尝试为整个测试集复用同一个连接,这样可以减少每次测试获取新连接的开销.从1,1版以来,DatabaseTestCase在setUp()和tearDown()中都会关闭连接.你可以覆盖closeConnection()方法,在方法体无需写任何代码,这样可以避免关闭连接.

In-container with Cactus or JUnitEE

容器内使用Cactus or JUnitEE做测试

If you use the in-container strategy you should use the DatabaseDataSourceConnection class to access the DataSource you configured for your application server. JDBC connections are requested on demand from the DataSource. So you can rely on the built-in connection pooling capability of your application server to achieve good performance.

IDatabaseConnection connection = new DatabaseDataSourceConnection(
new InitialContext(), "jdbc/myDataSource");

如果你使用容器内测试策略,那么你应该使用DatabaseDataSourceConnection类访问你在应用服务器
配置的数据源.你可以从数据源获取JDBC连接.所以你能依赖应用服务器内建的连接池获取更好的性能.代码如下:
IDatabaseConnection connection = new DatabaseDataSourceConnection(
new InitialContext(), "jdbc/myDataSource");

注:
以上是dbunit官方网站的最佳实践,英语水平有限,翻译的质量多包涵。
本人对于最佳实践的第一条和第二条不敢苟同,我会在我的blog阐述我的个人观点

并总结dbunit的实践经验
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics