cs0815
cs0815

Reputation: 17418

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 Configuration();  
cfg.Configure();  
cfg.AddAssembly("Bla"); 
new SchemaExport(cfg).Execute(false, true, false);

Upvotes: 0

Views: 261

Answers (5)

Fabio Maulo
Fabio Maulo

Reputation: 466

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

Upvotes: 2

Shane Courtrille
Shane Courtrille

Reputation: 14097

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).

Upvotes: 0

TheBoubou
TheBoubou

Reputation: 19933

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.

Upvotes: 0

Lachlan Roche
Lachlan Roche

Reputation: 25956

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.

Upvotes: 0

Paco
Paco

Reputation: 8381

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.

Upvotes: 0

Related Questions