Click here to Skip to main content
Licence CDDL
First Posted 13 Jan 2010
Views 5,358
Bookmarked 6 times

Database Driven Unit Testing Using VSTS

By | 13 Jan 2010 | Technical Blog
The primary concern about Database driven Unit testing is to maintain the test database intact. To elaborate suppose we have an add method that adds customer information in the database and we have a get method that actually returns a list of customer from database.So we will have to different Unit
A Technical Blog article. View original blog here.[^]

The primary concern about Database driven Unit testing is to maintain the test database intact. To elaborate suppose we have an add method that adds customer information in the database and we have a get method that actually returns a list of customer from database.

So we will have to different Unit test method for AddCustomer() and GetCustomer().

Now in order to pass the GetCustomer() method as a test case we have decided that it will return exactly 3 records and we have setup our test database in such a way to meet our test criterion.

The problem will be raised if we test the AddCustomer() method before GetCustomer() Method as AddCustomer() method will add one customer and our GetCustomer() Method will return 4 records that will fail its test case.

In order to overcome such situation we can use the power of TransactionScope Object.

In order to use this you must include System.Transactions in the Unit Test Project references and add System.Transactions in the reference section.

In the class generated by VSTS  Unit Test wizard you can see one region commented like below.

#region Additional test attributes 

//You can use the following additional attributes as you write your tests: 
 
//Use ClassInitialize to run code before running the first test in the class 
//[ClassInitialize()] 
//public static void MyClassInitialize(TestContext testContext) 
//{ 
//} 
 
//Use ClassCleanup to run code after all tests in a class have run 
//[ClassCleanup()] 
//public static void MyClassCleanup() 
//{ 
//} 
 
//Use TestInitialize to run code before running each test 
//[TestInitialize()] 
//public void MyTestInitialize() 
//{ 
//} 
 
//Use TestCleanup to run code after each test has run 
//[TestCleanup()] 
//public void MyTestCleanup() 
//{ 
//} 

#endregion

From here just uncomment MyTestInitialize() and MyTestCleanup() method.

Now Declare one global variable of TransactionScope Object write the body of the methods like below

TransactionScope ts; 

///Use TestInitialize to run code before 

///running each test 

[TestInitialize()] 

public void MyTestInitialize() 

{ 

ts = new TransactionScope(); 

} 

///Use TestCleanup to run code after 

///each test has run 

[TestCleanup()] 

public void MyTestCleanup() 

{ 

ts.Dispose();
}

After this before executing any Unit Test method MyTestInitialize() method will be called where a new instances of TransactionScope will be created and and after executing the Unit test method the object will be disposed which will ultimately rollback all the changes committed by the Unit test Method keeping our test database intact.

Happy Testing!

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

About the Author

Mahmudul Haque Azad

Software Developer (Senior)
Vizrt Bangladesh
Bangladesh Bangladesh

Member

Follow on Twitter Follow on Twitter
I am truly versatile and 360 degree Engineer having wide range of development experience in .NET and Java Platform. I am also proficient in system level programming in C++. To me technology is not at all a problem, it’s the client requirement that matters! That is I am ready and comfortable to use any technology to make the business of my client a success.
 
In my five years of experience I have the opportunities to work for fortune 500 companies of US and many renowned clients from Europe.
 
My Linkedin Profile: http://bd.linkedin.com/in/mahmudazad

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 14 Jan 2010
Article Copyright 2010 by Mahmudul Haque Azad
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid