单元测试服务层 - NUnit、NHibernate
我想对依赖服务层进行单元测试,它允许我执行 CRUD 操作,而无需使用 NUnit 进行模拟。我知道这可能是不好的做法,但无论如何我想尝试一下 - 即使测试必须整夜运行。我的数据是使用 NHibernate 保存的,并且我已经实现了一个小库来“引导”数据库,我可以在 [Setup] 方法中使用它。我只是想知道是否有人做过类似的事情以及引导数据库最快的方法是什么。我正在使用这样的东西:
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly("Bla");
new SchemaExport(cfg).Execute(false, true, false);
建立数据库模式。之后,我从一些 Excel 表中填充一些查找表。
任何反馈将不胜感激。谢谢。
基督教
I would like to unit test a DEPENDENT service layer which allows me to perform CRUD operation without mocking using NUnit. I know this is probably bad practice but I want to give it a try anyway - even if the tests have to run over night. My data is persisted using NHibernate and I have implemented a little library that 'bootstraps' the database which I could use in a [Setup] method. I am just wondering if someone has done something similar and what the fastest method for bootstrapping the database is. I am using something like this:
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly("Bla");
new SchemaExport(cfg).Execute(false, true, false);
to establish the db schema. After that I populate some lookup tables from some Excel tables.
Any feedback would be very much appreciated. Thanks.
Christian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ayende 在这篇 博客文章中提供了这种方法的一个很好的例子。他的例子和我的评论如下所示。
由于创建配置的成本很高,因此每次测试运行仅创建一次。使用 SQLite 内存数据库,因为这是执行查询的最快方法。
使用此功能时,每个测试都从创建所需的数据开始。
Ayende has a good example of this approach in this blog post. His example is shown below with my comments.
As the Configuration is expensive to create, it is created only once per test run. A SQLite in-memory database is used as this is the fastest way to do queries.
When using this, each test starts by creating required data.
在编写此类集成测试时,要记住的最重要的事情是您仍然应该
我写过我自己的数据库测试经验 过去有过几次。
When writing such Integration Tests, the most important things to keep in mind that you should still
I have written about my own experiences with testing against databases several times in the past.