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!
CodeProject
Modifying GhostDoc XML Comment
Today while browsing I came across Matthew M. Osborn’s Blog and I found this fantastic line
“Documentation is the coders best and worst friend. It is a two sided sword, it makes your life easier but no one likes writing it. So coders do what we do best and find a way to make the computer do it for us.”
Ideally I use GhostDoc to make comments for my codes and really it works fine for me. From the very first day since I have been using this, I always thought whether I can change the xml documentation as per my need or not.
Today I have found a simple trick to add to custom node to the xml commenting.
Here was my scenario,
All my method/class/event level comment must include author name along with the time when it was written.
For this I need to add the following lines in the Ghost Doc > Options > Custom Texts
<author>$(Environment.UserName)</author>
<datetime>$(Environment.Date)-$(Environment.Time)</datetime>
And the ghost doc generated comment contains author and datetime tag!!!
///<summary>
/// Gets the user by ID.
///</summary>
///<param name=”ID”>The ID.</param>
///<returns></returns>
///<author>Mahmudul.Haque</author>
///<datetime>3/15/2010-11:23 AM</datetime>
private static User GetUserByID(long ID)
{
User user = new User();
return user;
}