Sponsored Ad

Wednesday, January 6, 2010

Unit Testing using Dummy Datatable, An Additional Approach

The main purpose of unit testing is to take the smallest piece of testable application in the application, isolate it from the rest of the code and decide if it behaves exactly as expected. Each unit is tested separately before integrating them into the modules to check the interfaces between modules. Unit testing has proven its value in which a huge percentage of defects identified during use.

One simple approach is  to make a dummy dataset and data table, give the same column name and try to fill similar kind of values in dataset and table. now use this dataset as sample and test your code with this input.

Please let us know if you feel any problem in making a dataset and testing with it. I like queries and questions. Please ask me.

 

Code Snippet
  1. public DataTableGetUTLogs()
  2.       {
  3.           DataTable dt = new DataTable();
  4.           try
  5.           {
  6.               //Log_Id
  7.               dt.Columns.Add(new DataColumn("log_id",Type.GetType("System.String")));                
  8.               //Log_Type
  9.               dt.Columns.Add(new DataColumn("log_type",Type.GetType("System.String")));
  10.               //name
  11.               dt.Columns.Add(new DataColumn("name",Type.GetType("System.String")));
  12.  
  13.               dt.AcceptChanges();
  14.               DataRow dr = dt.NewRow();
  15.  
  16.               //First Row
  17.               dr["log_id"]="12379";
  18.               dr["name"]="xyz inc.";
  19.               dr["log_type"]="ON";
  20.               
  21.               dt.Rows.Add(dr);
  22.               dt.AcceptChanges();
  23.  
  24.               //Second Row
  25.               dr=null;
  26.               dr=dt.NewRow();
  27.               dr["log_id"]="13380";
  28.               dr["name"]="usa inc.";
  29.               dr["log_type"]="OFF";
  30.               
  31.               dt.Rows.Add(dr);
  32.               dt.AcceptChanges();
  33.  
  34.               //Third Row
  35.               dr=null;
  36.               dr=dt.NewRow();
  37.               dr["log_id"]="12281";
  38.               dr["name"]="abcd inc.";
  39.               dr["log_type"]="ON";
  40.               
  41.               dt.Rows.Add(dr);
  42.               dt.AcceptChanges();
  43.           
  44.               return dt;
  45.           
  46.           }
  47.           catch(Exception ex)
  48.           {
  49.               throw;
  50.           }
  51.           finally
  52.           {
  53.               if(dt != null)
  54.               {
  55.                   dt =null;
  56.               }
  57.           }
  58.       }

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers