How do I test database-related code with NUnit?

Posted by Michael Haren on Stack Overflow See other posts from Stack Overflow or by Michael Haren
Published on 2008-11-26T15:51:01Z Indexed on 2010/05/07 18:08 UTC
Read the original article Hit count: 325

Filed under:
|
|
|

I want to write unit tests with NUnit that hit the database. I'd like to have the database in a consistent state for each test. I thought transactions would allow me to "undo" each test so I searched around and found several articles from 2004-05 on the topic:

These seem to resolve around implementing a custom attribute for NUnit which builds in the ability to rollback DB operations after each test executes.

That's great but...

  1. Does this functionality exists somewhere in NUnit natively?
  2. Has this technique been improved upon in the last 4 years?
  3. Is this still the best way to test database-related code?


Edit: it's not that I want to test my DAL specifically, it's more that I want to test pieces of my code that interact with the database. For these tests to be "no-touch" and repeatable, it'd be awesome if I could reset the database after each one.

Further, I want to ease this into an existing project that has no testing place at the moment. For that reason, I can't practically script up a database and data from scratch for each test.

© Stack Overflow or respective owner

Related posts about nunit

Related posts about unit-testing