NUnit testing & database setup

  • Thread starter Thread starter Kiran
  • Start date Start date
K

Kiran

Hi,

I am looking for a way to setup my databases for my unit test case to run.
basically my class needs some test environment to me available before I can
test it, and if I have to write code to create that test environment....
then its will be a big task by itself.

Instead one possible solution I can think of is to use UI and set up the
test environment and back up the database either as MS Access database or
text(CVS) files and before starting my test case import this in the
testsetup operation.

Dose any one here has a Idea how this can be achieved using C# and MS SQL.
i.e. how I can import the data programmatically.

Or is there a better way to handle my situation?

Regards
Kiran
 
Kiran,

I've done NUnit testing on a database, and here's what I do (its far from
perfect!);

I include a CreateDatabaseTables.sql script in the solution (so other
developers can get it), and in all the tests I first clear all the data
(usually in a simple base test class or [SetUp] method), then create
appropriate test data for that test and do the test.

This way each test is not dependant upon any other test, and they all start
from a fresh database. But if you need lots of test data it could get slow.

As you increase the number of test hopefully you should see common test code
appear and which can be refactored into common code - usually for the
creation of test data. i.e. a CreateTestInvoice method etc.

I've tried to make a database with a set of initial data, but I had problems
where test became dependant upon the order which they ran - which is a big
pain.

I have thought about storing a initial set of data outside of the database
which is 'loaded' before each test is run - but I've never had the drive
from an application to do it. I did consider using XML to define the data as
well as a backup file, hadn't thought about using Access though!

Adam
 
Adam,

Firstly Thanks for the quick reply.
I include a CreateDatabaseTables.sql script in the solution (so other
developers can get it), and in all the tests I first clear all the data
(usually in a simple base test class or [SetUp] method), then create
appropriate test data for that test and do the test.

I did think about this, but the data I need is bit too complex to write set
of SQL statement to create. Thats why I mentioned that I want to back up my
Initial state and use it.
I've tried to make a database with a set of initial data, but I had problems
where test became dependant upon the order which they ran - which is a big
pain.

I also plan to undo the changes I did via import, currently I can simply
restore the previous state (i.e. if I get to see how this can be dome from
my c# code)
I have thought about storing a initial set of data outside of the database
which is 'loaded' before each test is run - but I've never had the drive
from an application to do it. I did consider using XML to define the data as
well as a backup file, hadn't thought about using Access though!

Well, I don't mind XML or any other format as long as all the data is in one
place for easy VSS management.

Regards
Kiran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top