1

Currently, I have a unit test framework where I'm using a SQLite database in memory with NHibernate for unit testing. Each unit test runs in it's own session to ensure independence. For the most part, this works awesome, but as I'm approaching 150 tests, it's starting to run a little slow. The main reason for this, is that prior to each test I have to insert a good bit of data in the database.

Basically my application is a workflow app, and we store alot of information about the workflow (number of steps, fields on each step, etc.) in the database. The application can't run unless this stuff is there, so it's required for my tests.

At this point about 4 minutes of my total test time is spent on populating the database before each unit test. I'd love to make this population happen once per test execution, but since the in memory database is destroyed when the session is closed, I'm not sure how. Any suggestions?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AndrewSwerlick
  • 913
  • 8
  • 24
  • Mock your repositories instead. Get the repositories to return the entity objects required by your tests. This is very quick. I have a project with over 900 tests that runs in well under a minute. – Jack Hughes Oct 25 '12 at 13:43
  • That's a possibility, but it would require alot of additional code. The advantage I have right now by directly using the database is that my deployment scripts and my test setup code are the same. I also haven't bothered with a strict repository setup, instead following ayende's guidance here http://ayende.com/blog/3955/repository-is-the-new-singleton. As a result I interact with the ISession directly in a number of situations. Granted, it will not be too tough to and in a simple abstraction layer that could be mocked, but I was hoping for something simpler. – AndrewSwerlick Oct 25 '12 at 14:03

1 Answers1

4

You could copy a pre-filled database file to a temporary directory (on a RAM disk, if possible).

Alternatively, you can copy the contents of a database into your memory database by using the SQLite backup API.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • That would require using a file based sqlite database, which while a potential solution is not ideal. Even if it were on a ramdisk, I'd still have to do some work to make sure that the tests would run fine for everyone else on the team who isn't running with a ramdisk setup. I looked into the SQLite backup api but as far as I can tell it's not something you can manipulate via the C# library for SQLite. If you have any information to the contrary though, that would be great. – AndrewSwerlick Oct 25 '12 at 16:38
  • System.Data.SQLite [does support the backup API](http://stackoverflow.com/questions/11383775/c-sharp-sqlite-memory-stream-as-db). – CL. Oct 25 '12 at 17:06
  • awesome, I must have been looking at outdated documentation. I'll test this out over the weekend and accept if I get it working. Thanks for your help. – AndrewSwerlick Oct 25 '12 at 18:22
  • This worked perfectly. My tests went from 3-5 minutes to run to approximately 20 seconds. Thanks so much. – AndrewSwerlick Oct 26 '12 at 03:20