开发者

What is the fastest way to clear out a database using NHibernate?

I intend to perform some automated integration tests. This requires the db to be put back into a 'clean state'. Is this the fastest/best way to do this?

var cfg = new Configurat开发者_如何转开发ion();  
cfg.Configure();  
cfg.AddAssembly("Bla"); 
new SchemaExport(cfg).Execute(false, true, false);


            var se = new SchemaExport(conf);
        se.Drop(false, true);
        se.Create(false, true);


Yes it almost is. You don't have to create a new configuration object before each test, you can reuse it when created once. You can make it faster by using an inmemory database like sql-lite for testing.


My integration tests do SessionFactory creation in a base class constructor, and SchemaExport in test fixture setup. I also test against SQLite running as an in-memory database for extra speed.

Ayende gave a good example of this approach in this blog post. Tobin Harris' article includes timing data of drop/create vs delete.


Me I use Proteus, it's an open source library. Before each test, there is an auto save of your set of data , load the set you want to test (an empty DB for exemple). After each test, the set of data is reloaded after the last test, the data present in the database before the tests are restored.


Since NHibernate is database independent another interesting option if you are having it generate your database is to run your tests against something like SQLite which is in memory. Things will run MUCH faster that way.

Here is an article on showing how to do this with ActiveRecord but you can take the concept and use it without ActiveRecord.

And here is a discussion if you're having trouble getting it working (I did at first).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜