Mhazad's Blog

January 5, 2010

Database Driven Unit Testing Using VSTS

Filed under: ASP.NET, Software Testing, Visual Studio — Mahmudul Haque Azad @ 7:14 am

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


//public static void MyClassInitialize(TestContext testContext)




//Use ClassCleanup to run code after all tests in a class have run


//public static void MyClassCleanup()




//Use TestInitialize to run code before running each test


//public void MyTestInitialize()




//Use TestCleanup to run code after each test has run


//public void MyTestCleanup()





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


void MyTestInitialize()


ts = new


///Use TestCleanup to run code after

///each test has run


void MyTestCleanup()




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!


September 8, 2009

Magic of JQuery in ASP.NET, Simplifying AJAX

Filed under: AJAX, ASP.NET, JQuery — Tags: , , , , — Mahmudul Haque Azad @ 4:58 pm

JQuery is a powerful tool that can be used to enhance our ASP.NET Application.

In the time of development we often face problem like “need to show certain data based on a button click” or “need to implement tab in pages where each tab contains costly DB operation”.

There is lots of way to do this using traditional AJAX like ASP.NET AJAX. But if we consider the hassle related with UpdatePanel then we can find a few people who will go for it.

Let’s see how we can achieve this short of Magic in our application easily with JQuery!

For simplicity let’s assume that we have one aspx page with one div and one button. After clicking the button one extensive database operation will be held and the div’s inner html will be updated without page refresh.

It’s to be noted that in order to leverage this facility of accessing Page Method through client script we need to add script manger and need to enable page method.

<asp:ScriptManager ID="ScriptManager1" runat="server"

<asp:panel ID="divExtensiveData" runat="server"></asp:panel>

<asp:Button ID="btnFetchData" Text="Fetch Data" runat="server" OnClientClick="fetchData(); return false;" />

Now let’s write one method in code behind i.e. in aspx.cs file that will do the actual db operation.

public static string fetchData(string someParameter)
  string result = string.Empty;

  /// Do extensive db operation
/// Assign value to result
  return result;

Now we will get back to our aspx page and will write one method to access this page method.

<script type="text/javascript">
function fetchData(parameter)

function dataFetched(result)
 var panelID = '<%=divExtensiveData.ClientID %>';
function dataNotFetched()
 var panelID = '<%=divExtensiveData.ClientID %>';
$("#"+panelID).html("<h1>Error Occured.</h1>");

Now explaining the page method call from script. In our call to page method  I have given three parameters whereas the page method takes only one. The second one is just  a reference of the method that will be executed after successfully page method call and the third one is the reference of the method that will be called if the page method encounter some error.

Just use this concept and be a magician of JQuery in ASP.NET!!! CodeProject

Create a free website or blog at