Re: Best Way to Test Against a Database

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi,

We use transactions and the [Setup] and [TearDown] methods in NUnit.
You can begin the transaction in the Setup method, test whatever you
like to test in your test
and rollback the transaction in your TearDown method. If anything goes
wrong the db will complain.

If we want to test the result of a query, we do the same, but in our
test method we commit, assert the result, restore the database
by another commit and then begin a new transaction, in order to have
this empty transaction rolled back in the teardown method
and avoid an exception in the TearDown method.

If you have many testfixtures, you can derive all of them from a base
fixture, which for example begins and rolls back the transaction in
Setup and TearDown, so you
don't need to code this for each testfixture.

Joachim
.