数据库基本单元测试策略:在单元测试和测试数据 [英] Database base unit testing strategy: truncating tables between unit tests & test data

查看:181
本文介绍了数据库基本单元测试策略:在单元测试和测试数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JUnit测试一些数据库代码。我很新鲜,所以请忍受我。

I'm attempting to use JUnit to test some database code. I'm pretty new to this so please bear with me.

我有一个本地测试数据库,由四个表组成。其中两个表填充数据,另外两个表在程序执行之前为空。

I have a local test database which consists of four tables. Two of these tables are populated with data and the other two are empty before the program executes.

程序基本上对填充的两个表执行一些查询,处理结果然后将这些结果写入在执行程序时为空的两个表。有很多方法可以读写db。我想测试这些。

The program basically performs some queries on the two tables which are populated, processes the results then writes those results to the two tables which are empty when the program is executed. There are a number of methods which read and write to the db. I would like to test these.

我想要的是tearDown()方法截断当程序执行时为空的表,以便他们准备好下一个测试。

What I would like is for the tearDown() method to truncate the tables which are empty when the program executes so that they are ready for the next test. What is the best way of achieving this?

此外,包含数据的表可能会在开发人员每次设置测试环境时通过运行SQL脚本来创建。有没有更好的方法这样做?它可以自动化吗?将有相当多的数据,所以我真的不想为每个表手动创建XML数据集。

Also, the tables which contain the data will probably be created by running SQL script each time a developer sets up a test environment. Is there a better way of doing this? Could it be automated? There will be quite a lot of data so I don't really fancy manually creating XML datasets for each table.

我使用的是Oracle数据库。

I'm using an Oracle database.

非常感谢您提供任何建议/建议。

Many thanks for any advice/suggestions you can offer.

推荐答案

尝试在测试中使用 DBUnit 框架。这是JUnit的扩展,并提供您需要的功能。设置它可能有点困难,但一旦设置它的工作方式像一个魅力。

Try using the DBUnit framework in your tests. This is an extension to JUnit and provides the functionality you need. Setting it up can be a bit hard, but once set up it works like a charm.

这篇关于数据库基本单元测试策略:在单元测试和测试数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆