Test-driven development approach for database applications


This is the fourth article of a series of articles related to the Test-driven development (TDD) approach in Microsoft.NET. My intention is to illustrate this approach with several real-world examples.
In this article, I will show how programmers can use the test-driven development approach to test enterprise solutions and its underlying data layer which should also support transactions in order to manage persistent data and leave the database systems incorrect states. The data access layer components are implemented using ADO.NET technologies.
Getting started with the solution
It's remarkable to say that we have to face several challenges when testing applications with a data access layer. In the first place, it takes longer to run tests associated with persistent data in a database than it does to run tests on in-memory data. Secondly, database systems ensure that persistent data is consistent with the schema definition. This consistency checking is an issue when writing tests when we want to test each business entity without having to create all the supporting entities and finally we have to create all the supporting entities just to test the individual entity. Thirdly, you should not rely on existing data on the database (it might be manipulated by other developers).
Thus, for each test, you should insert into the database whatever is needed for the test, run the test, and then remove anything that was inserted before. And finally, you should not run your test in the production database. You should take a snapshot of the production database and run the test against it.
As an illustrative example, we're going to develop the data access layer as a Class Library project and the underlying data access components using ADO.NET and strongly typed data set object model (see Figure 1).
Figure 1
You're going to use a very simple database schema for testing purposes. The database schema is made up of the dept and emp tables and an optional non-identifying relationship between these tables. These tables store the state of the department and employee business entities as shown in Figure 2.
Figure 2
Now we're going to add a DataSet item to represent the underlying business entities and the logic to access the data in the database systems (see Figure 3).
Figure 3
The first step in the test-driven development approach is, of course as its name implies, the formulation of a list of tests. One important thing to keep in mind is that the list of tests is dynamic in order to add or remove tests for testing in different environments.
In this case, the list test case is as follows:
  1. To connect to the database.
  2. To test CRUD operations for the tables.
  3. To test the relationship between them.
In order to implement these test cases, we have to define the strongly typed data set for the employee and department business entities (see Figure 4).
Figure 4
Now let's add the test project as a Class Library project (see Figure 5).
Figure 5
Then, you have to add a reference to the NUnit framework (see Figure 6) and a fixture class (see Figure 7).
Figure 6
Figure 7
Then you must also add a reference to the TDD_DataAccessLayerPkg assembly (see Figure 8).
Figure 8
If you don't want to hard code the connection string in order to be used in the test library in several environments, you have to add a configuration file with the following configuration (see Listing 1).
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.       <configSections>  
  4.       </configSections>  
  5.       <connectionStrings>  
  6.             <add name="TDD_DataAccessLayerPkg.Properties.Settings.TestDBConnectionString"  
  7.             connectionString="Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True"  
  8.             providerName="System.Data.SqlClient" />  
  9.       </connectionStrings>  
  10. </configuration> 
Listing 1
Once you have the configuration, and one way is to pass this external information to the application using the ConfigurationSettings class. In order to not duplicate the retrieval of the connection string from the configuration file, we need to write the underlying code to a method annotated with the SetUp Attribute which ensures that this code is called prior to each test.
In order to test each persistent business entity, we need to firstly add an entity into the database, retrieve it (check that's the same entity), and then delete it after we are finished. To retrieve a particular entity from the database, we need to add a new method to the table adapter with the necessary SQL code.
The SQL code for the dept table is shown in Figure 9.
Figure 9
The SQL code for the emp table is shown in Figure 10.
Figure 10
Now it's time to write code for the test cases (to test the CRUD operations) using the test methods TestEmployee and TestDepartment annotated with Test attribute. We also need to add helper methods that are invoked from the test cases (see Listing 2).
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Configuration;  
  5. using System.Data;  
  6. using System.Data.SqlClient;  
  7. using NUnit.Framework;  
  8. using TDD_DataAccessLayerPkg;  
  9. using TDD_DataAccessLayerPkg.DS_TestTableAdapters;  
  11. namespace TDD_DataAccessLayer_TestPkg {  
  12.     [TestFixture]  
  13.     public class DS_TestFixture {  
  14.         private string m_strConnString = null;  
  16.         [SetUp]  
  17.         public void GetConnectionString() {  
  18.             this.m_strConnString = ConfigurationManager.ConnectionStrings["TDD_DataAccessLayerPkg.Properties.Settings.TestDBConnectionString"].ConnectionString;  
  19.         }  
  21.         [Test]  
  22.         public void ConnectionIsOpen() {  
  23.             SqlConnection objConn = new SqlConnection(this.m_strConnString);  
  24.             objConn.Open();  
  25.             Assert.AreEqual(ConnectionState.Open, objConn.State);  
  26.             objConn.Close();  
  27.         }  
  29.         public static void TestInsertEmployee(int nEmpNo, string strName, decimal dSalary, SqlConnection objConn) {  
  30.             DS_Test dsInstance = new DS_Test();  
  31.             DS_Test.empRow objEmp = dsInstance.emp.NewempRow();  
  32.             objEmp.empno = nEmpNo;  
  33.             objEmp.ename = strName;  
  34.             objEmp.salary = dSalary;  
  35.             dsInstance.emp.AddempRow(objEmp);  
  37.             empTableAdapter taEmp = new empTableAdapter();  
  38.             taEmp.Connection = objConn;  
  39.             taEmp.Update(dsInstance);  
  40.         }  
  41.         public static void TestSelectEmployee(int nEmpNo, string strName, decimal dSalary, SqlConnection objConn) {  
  42.             DS_Test dsInstance = new DS_Test();  
  43.             empTableAdapter taEmp = new empTableAdapter();  
  44.             taEmp.Connection = objConn;  
  45.             taEmp.FillBy(dsInstance.emp, nEmpNo);  
  46.             DS_Test.empRow objEmp = dsInstance.emp[0];  
  48.             Assert.AreEqual(nEmpNo, objEmp.empno);  
  49.             Assert.AreEqual(strName, objEmp.ename);  
  50.             Assert.AreEqual(dSalary, objEmp.salary);  
  51.         }  
  52.         public static void TestDeleteEmployee(int nEmpNo, SqlConnection objConn) {  
  53.             DS_Test dsInstance = new DS_Test();  
  54.             empTableAdapter taEmp = new empTableAdapter();  
  55.             taEmp.Connection = objConn;  
  56.             taEmp.FillBy(dsInstance.emp, nEmpNo);  
  57.             DS_Test.empRow objEmp = dsInstance.emp[0];  
  59.             Assert.AreEqual(nEmpNo, objEmp.empno);  
  61.             objEmp.Delete();  
  62.             taEmp.Update(dsInstance);  
  63.         }  
  65.         public static void TestInsertDepartment(int nDeptNo, string strName, string strLocation, SqlConnection objConn) {  
  66.             DS_Test dsInstance = new DS_Test();  
  67.             DS_Test.deptRow objDept = dsInstance.dept.NewdeptRow();  
  68.             objDept.deptno = nDeptNo;  
  69.             objDept.dname = strName;  
  70.             objDept.loc = strLocation;  
  71.             dsInstance.dept.AdddeptRow(objDept);  
  73.             deptTableAdapter taDept = new deptTableAdapter();  
  74.             taDept.Connection = objConn;  
  75.             taDept.Update(dsInstance);  
  76.         }  
  77.         public static void TestSelectDepartment(int nDeptNo, string strName, string strLocation, SqlConnection objConn) {  
  78.             DS_Test dsInstance = new DS_Test();  
  79.             deptTableAdapter taDept = new deptTableAdapter();  
  80.             taDept.Connection = objConn;  
  81.             taDept.FillBy(dsInstance.dept, nDeptNo);  
  82.             DS_Test.deptRow objDept = dsInstance.dept[0];  
  84.             Assert.AreEqual(nDeptNo, objDept.deptno);  
  85.             Assert.AreEqual(strName, objDept.dname);  
  86.             Assert.AreEqual(strLocation, objDept.loc);  
  87.         }  
  88.         public static void TestDeleteDepartment(int nDeptNo, SqlConnection objConn) {  
  89.             DS_Test dsInstance = new DS_Test();  
  90.             deptTableAdapter taDept = new deptTableAdapter();  
  91.             taDept.Connection = objConn;  
  92.             taDept.FillBy(dsInstance.dept, nDeptNo);  
  93.             DS_Test.deptRow objDept = dsInstance.dept[0];  
  95.             Assert.AreEqual(nDeptNo, objDept.deptno);  
  97.             objDept.Delete();  
  98.             taDept.Update(dsInstance);  
  99.         }  
  101.         [Test]  
  102.         public void TestEmployee() {  
  103.             SqlConnection objConn = new SqlConnection(this.m_strConnString);  
  104.             objConn.Open();  
  106.             TestInsertEmployee(7380, "John Charles", 100, objConn);  
  107.             TestSelectEmployee(7380, "John Charles", 100, objConn);  
  108.             TestDeleteEmployee(7380, objConn);  
  110.             objConn.Close();  
  111.         }  
  113.         [Test]  
  114.         public void TestDepartment() {  
  115.             SqlConnection objConn = new SqlConnection(this.m_strConnString);  
  116.             objConn.Open();  
  118.             TestInsertDepartment(50, "IT""Baltimore", objConn);  
  119.             TestSelectDepartment(50, "IT""Baltimore", objConn);  
  120.             TestDeleteDepartment(50, objConn);  
  122.             objConn.Close();  
  123.         }  
  124.     }  
Listing 2
Now let's test the cases using the GUI NUnit test runner. Right-click on the project and select Properties from the context menu. In the Debug tab, set the GUI NUnit test runner (see Figure 11).
Figure 11
Now let's build the solution and run the test. When the GUI NUnit test runner is run for the first time, we need to load the test project (see Figure 7).
Figure 12
In the GUI NUnit test runner window, click on the Run button to start the test, and finally, you can see that all the test cases have passed (see Figure 13).
Figure 13


In this article, I've illustrated how programmers can use the test-driven development approach to implement and test database applications. Now you can apply this approach to your own business solutions.