数据库单元测试框架?
在我的项目中,我使用了 spring、jpa 和 PostgreSQL DB, 我在数据库中有很多表,我需要对所有表进行单元测试。
是否有任何框架在每次测试完成后回滚所有事务,以便每个测试都将有新鲜/相同的数据库数据进行测试。这样在所有测试执行之后,数据库模式的数据将保持原样。
对此有何建议?
我对 DBUnit 有一些想法,但我需要为每个测试的每个输入数据编写 .xml 文件,并且需要在 setup() 中插入数据并在 TeaDown() 中清除/删除数据,但似乎没有更好的策略大部头书。
任何建议表示赞赏。 谢谢。
In my project, I've used spring, jpa with PostgreSQL DB,
I've lots of table in DB and I need to have Unit testing of all of them.
Is there any framework which just rollback all the transactions after each test finished so every test will have fresh/same DB data to Test. And this way after all Test executions, data of DB schema would be as it is.
Any suggestion for this?
I've some idea of DBUnit but in that I need to write .xml files for every input data for every test and need to insert data in setup() and clear/remove data in tearDown(), but doesn't seems better strategy to me.
Any suggestion is appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如@Ryan所指出的......Spring参考的 测试部分应查阅手册。
一些启动技巧...
我们已经使用 Spring 的
AbstractTransactionalJUnit4SpringContextTests
处理了这个问题。例如,我们定义一个抽象超类:
然后将需要附加上下文的各个子类定义为:
请注意:
@ContextConfiguration
。junit
任务上使用forkmode="perBatch"
属性。这可确保所有测试都使用相同的上下文配置运行(无需为每个测试重新加载 Spring 上下文)。您可以使用 @DirtiesContext 来指示应在方法/类之后刷新上下文。@Test
注释标记每个方法。 Spring 框架不会使用 Junit 的public void testXXX()
约定来选取方法。As @Ryan indicates .... the Testing section of the Spring Reference manual should be consulted.
Some startup tips...
We've handled this using Spring's
AbstractTransactionalJUnit4SpringContextTests
.For example, we define an abstract superclass:
And then individual subclasses which need additional context get defined as:
Note that:
@ContextConfiguration
on the particular subclass.forkmode="perBatch"
attribute on thejunit
task. That ensures all tests run with the same context configuration (saves from reloading the Spring context for each test). You can use the@DirtiesContext
to indicate that the context should be refreshed after a method/class.@Test
annotation. The Spring framework doesn't pick up methods using Junit'spublic void testXXX()
convention.来自我的其他答案之前发布的天,是的,这可以使用 DbUnit 实现。 (根据您的编辑,您不需要这个;我的答案的后续部分说明了我使用 DbUnit 的原因以及何时不使用它)。
以下代码片段演示了如何执行每个测试的设置:
database-test-setup.xml
文件包含将为每个测试插入数据库的数据。在setup
方法中使用DatabaseOperation.CLEAN_INSERT
可确保清除文件中指定的所有表(通过删除所有行),然后插入测试数据文件中的指定数据。避免 DbUnit
我使用上述方法专门在每次测试开始之前清除序列,因为应用程序使用 JPA 提供程序在单独的事务中更新序列。如果您的应用程序没有执行类似的操作,那么您可以在
setup()
方法中简单地启动一个事务,并在测试后在拆卸时发出回滚。如果我的应用程序不使用序列(并且如果我不想重置它们),那么我的设置例程将非常简单:此外,我使用 dbdeploy 与 Maven 一起使用,但这主要是为了使测试数据库与版本化数据模型保持最新。
From my other answer posted earlier in the day, yes, this is possible using DbUnit. (Based on your edit, you don't need this; the subsequent section of my answer addresses why I use DbUnit, and when I wouldn't use it).
The following code snippet demonstrates how the setup of every test is performed:
The
database-test-setup.xml
file contains the data that will be inserted into the database for every test. The use ofDatabaseOperation.CLEAN_INSERT
in thesetup
method ensures that all the tables specified in the file will be cleared (by a delete of all rows) followed by an insert of the specified data in the test data file.Avoiding DbUnit
I use the above approach specifically to clear out sequences before the start of every test, as the application uses a JPA provider which updates the sequences in a separate transaction. If your application is not doing anything like that, then you can afford to simply start a transaction in your
setup()
method and issue a rollback on teardown after the test. If my application didn't use sequences (and if I didn't desire to reset them), then my setup routine would have been as simple as:Also, I use dbdeploy with Maven, but that is primarily for keeping the test database up to date with the versioned data model.
Spring 的测试框架 正是为你做的。
Spring's test framework does exactly that for you.
我是按照以下方式处理的。
当项目处于测试模式时。我使用引导数据来使用 dbdeploy 进行测试
您可以断言的固定数据。并直接使用
dao
来测试应用程序的DAO和DB层。希望它对
更新
有所帮助,例如您的系统中有一个名为
Person
的实体,现在您可以在其上测试基本的CRUD操作。来回滚您可以标记的事务,
这样它将回滚数据库内容
I'd handled it following ways.
When the project is in test mode. I'd used bootstraping data to to test using
dbdeploy
Fixed data that you can assert on. and use the
dao
directly to test the DAO and DB layer of your application.Hope it helps
Update
for example there is an entity called
Person
in your system, now what you can test on this is basic CRUD operations.to rollback the transaction you can mark
so it will rollback the DB stuff