views:

63

answers:

4

Hello,

I'm using ADO.NET entity framework, and am using an AdventureWorks database attached to my local database server. For unit testing, what approaches have people taken to work with a database?

Obviously, the database has to be in a pre-defined state of change so that the tests can have some isolation from each other... so I need to be able to run through the inserts and updates, then rollback either between tests or after the batch of tests are done.

Any advice?

Thanks.

A: 

You can use MbUnit which has a rollback attribute. This will make sure that all the changes made in the Test will be rolled back.

http://weblogs.asp.net/astopford/archive/2006/07/25/MbUnit-database-testing_2C00_-and-thanks-to-Roy.aspx

Amitabh
Does the rollback attribute play nicely with Entity Framework?
Eric J.
It uses System.Transaction. So it should work fine with EF
Amitabh
EF has surprised me in a lot of ways, sometimes pleasantly but often not :-) Still, certainly worth having a look at.
Eric J.
+1  A: 

Personally I have each test create it's own data and delete it when the test completes. This way, the data a test requires and the test itself are kept closely together and can be maintained as one unit. For data shared by a set of related tests (in the same *Test.cs file), I use [TestInitialize] Start() and [TestCleanup] Finish().

I also have a utility program that re-creates an empty test database, as the unit test's cleanup code will not run if I'm debugging a test and terminate it in the debugger. To re-create the database, I use EF 4's ObjectContext.CreateDatabase() method.

Eric J.
A: 

This is how we solved it:

  1. Use a Sandbox database in first place for development/testing
  2. Create all necessary data in the tests
  3. Delete all of the test data created by the tests after they pass/fail. Something like a try...finally
System.ArgumentException
+2  A: 

Hi Brian,

I have experienced a similar kind of situation.

Storing data to DB and retaining them to previous state are not ideally unit tests. They are Integration test cases.

In order to perform the integration tests (insert / update operations ) without affecting the actual data ,I would suggest the following

1. Use different database(change the name of the actual DB) , and let the schema remain the same as the actual DB.

2. Point the unit tests to the newly created DB.

3. Now how do we create a new DB , for running the tests alone in an automated way ?

Ans : Have the SQL scripts listed in a batch file , such as this.(here i presume you are using SQL SERVER)

E.g., sqlcmd -i c:\data\runscripts\InventoryMonthEnd.sql

Specify the batch file , which lodges this sql statemet(s) to be executed in MSBuild process.

Sample MSBuild Task .

<ItemGroup>
<StoredProcScripts Include="$(RelativeSPDir)/*.sql" />
</ItemGroup>

<Target>
<Exec Command="isql -E -n -b -d DBName -i %(StoredProcScripts.Identity)" Condition="'%(StoredProcScripts.Identity)' != ''"/>
</Target>

Hope this helps.

Thanks , Vijay.

vijaysylvester