Reputation: 23515
I'm just starting to understand the importance of Unit Testing in a c# environment. Now, I'm wondering how do i implement a black-box unit test that does Inserts,Deletes and updates on a database and then cleaning up the data after a successful test.
How do you actually do a process that rollbacks data inserted/updated/deleted? do you simply reset the index and remove the inserted rows? or restore the original state of the table by creating a script?
please guide me, I appreciate it. thanks!
Upvotes: 2
Views: 5324
Reputation: 1978
I was doing a research recently and found this thread as well. Here are my findings, which might be of some help for the future readers:
I personally use the latter and even ended up implementing a Reseed library, which does all the work for me.
Test frameworks usually do allow execute some logic before and after each test/test fixture run, which will mostlikely be needed for the ideas above. E.g for NUnit this implemented with use of OneTimeSetUp, OneTimeTearDown
, FixtureSetUp
, FixtureTearDown
, SetUp
, TearDown
attributes.
Upvotes: 1
Reputation: 10356
As long as your test is concise, and i presume it must be - for testing your DAL, why not just do the insert / update / deletes in a transaction that is rolled back once your test is complete.
Another option is to just use specific Update / Delete scripts in your test cleanup methods to roll back the exact changes that you updated / inserted to their pre-test values.
Upvotes: 3
Reputation: 3679
What we do here in our development cycle. we always have that unit testing and load testing in our mind when ever we are developing application. So we make a column in our every datadase's table with userId or else. Then when we run Load Test or Unit test we insert UserId -1 in that every column, pointing that it is a load test data and -2 in case of unit Test Data. then we have pre Define Jobs at data base end that will clean that data after some time.
Upvotes: 5
Reputation: 4764
I think deleting the rows in the CleanUp method should be good choice.
By this you will always be testing your deleting rows code.
Upvotes: 1
Reputation: 156188
One option is to use a Mock database in place of a real database. Here's a link that describes it.
Upvotes: 0