如何使用 dbUnit 将数据库恢复到初始状态?
我是自动化测试和 dbUnit 的新手。所以我很感激你的建议。
我将创建一个测试套件,它将按以下方式运行:
- 创建一个内存 H2 数据库
- 运行 DDL 脚本来创建表
- 运行 dbUnit 来插入初始数据(我们称之为 STATE0)被所有测试使用。
- 运行测试
直到那里对我来说看起来不错,但我不明白的是,在测试运行并更改数据后如何将数据库恢复到 STATE0 ?
我可以用 dbUnit 来做吗?
还是用别的东西?
我应该在每次测试之前重新创建数据库吗?
在测试中简单地不提交事务对我来说并不合适,因为测试最终将在可能多个数据库连接上运行多个事务。
I am new to automated testing and dbUnit. So I would appreciate your advice.
I am going to create a test suite, that will run the following way:
- create an in-memory H2 database
- run DDL scripts to create tables
- run dbUnit to insert initial data (let's call it STATE0) that will be used by all tests.
- run tests
Till there it looks nice for me, but what I don't understand, is how do I revert the database to the STATE0 after a test run and changed the data?
Can I do it with dbUnit?
Or with something else?
Should I recreate the database before each test?
Simple not commiting transactions in tests is not appropriate for me, because the tests will eventually run more than one transaction over may be more than one database connection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正确编写
@BeforeClass
、@Before
和@After
方法,DBUnit 可以自动完成这四个工作。例如,在我们的项目中,使用 Derby,这样的测试用例如下所示:此代码在每次测试后将数据库模式(的子集)恢复到由
MyDataSet.xml
定义的状态。 (请注意,正如 @Pascal 评论的那样,重置可能并不总是完整的 - 如果测试修改了不在数据集中的表,则它不会受到@Before
/@After
方法。)DBUnit can do the work four you automatically if you write your
@BeforeClass
,@Before
and@After
methods properly. E.g. in our project, using Derby, one such test case looks likeThis code puts back (a subset of) the DB schema to the state defined by
MyDataSet.xml
after each test. (Note that, as @Pascal commented, the reset may not always be full - if a test modifies a table which is not in the dataset, it won't be affected by the@Before
/@After
methods.)要将数据库初始化为初始数据集,只需在测试用例中实现这些方法:
如果某些测试在空表中插入行(例如未在初始数据集中定义),则可能有外键约束。
只需在数据集中添加这个没有任何行的空表:
这里,myemptytable 有一个指向 mypopulatedtable 的外键。如果未定义 myemptytable,DBUnit 将尝试删除 mypopulatedtable,但由于约束而失败。如果定义了,DBUnit 将删除之前的 myemptytable 行。
To initialize the database to the initial dataset, just implement these methods in your test case :
You may have foreign keys constraints if some of your tests insert rows in an empty table (not defined in initial dataset for example).
Just add this empty table in your dataset without any row :
Here, myemptytable has a foreign key to mypopulatedtable. If myemptytable was not defined, DBUnit would try to delete the mypopulatedtable but will fail because of the constraint. If defined, DBUnit will delete myemptytable rows before.