# Saturday, April 02, 2011

Unit testing certainly does give you the developer a warm fuzzy but that warm fuzzy can only get you so far.  In the end you still have to make sure all your little pieces of code/functionality play nice together.  This requires integration testing and if we are going to do any sort of testing that we actually expect to be performed on a regular basis we have to automate it.  Automated integration testing often gets neglected and is substituted with manual testing, which delays finding issues and adds time to the project timeline.  The reason that integration testing is often neglected is that it requires touching actual data. 

Why is this is an issue:

  1. Someone has to take the time create the data for the testing.
  2. The data has to be reset after each test run

All of this requires a lot of work, coordination…and a DBA [cringe]!

Let me introduce a scenario where all that is not required.  Why can’t we wrap our tests in a Transaction?

Here’s what I am thinking:

  1. Have a local empty copy of the database.
  2. Either in a test-by-test basis or test-suite basis
    1. Start a transaction (either at the start of your test or on build-up of the test suite)
    2. Create your data and put it in the DB
    3. Perform your tests on the data you just created and inserted
    4. Rollback the transaction
  3. … And Repeat

Here is an example:

  1.         [TestMethod]
  2.         public void GetAllEligible_ShouldReturn_All_Attendess_InThe_Database_Where_IsEligible_Equals_True() {
  3.             using (new TransactionScope()) {
  4.                 //Arrange
  5.                 int expectedCount = 1;
  6.                 Attendee attendee1 = new Attendee {FirstName = "Joe", LastName = "Smith", IsEligible = true};
  7.                 Attendee attendee2 = new Attendee {FirstName = "Randy", LastName = "Jones", IsEligible = false};
  8.                 using (var dc = new DayOfDotNetDataContext()) {
  9.                     dc.Attendees.InsertAllOnSubmit(new List<Attendee> {attendee1, attendee2});
  10.                     dc.SubmitChanges();
  11.                 }
  12.                 //Act
  13.                 IAttendeeRepository attendeeRepository = new AttendeeRepository();
  14.                 IList<AttendeeDTO> attendees = attendeeRepository.GetAllEligible();
  15.                 //Assert
  16.                 attendees.Count.ShouldEqual(expectedCount, "Wrong number of attendess");
  17.             }
  18.         }

You can see that the first line of my test starts a transaction scope.  I then create some data and add it to my empty database.  Now, remember this database needs to be empty as to not mess up your expectations.  After the data has been created I then perform the operation I am testing and assert the results.

You may be confused by the lack of a “Rollback” statement.  The way TransactionScope works is that it only commits when explicitly told to do so.

Automated integration testing is a must have and in order to make your life easier follow the steps I have outlined and you will be free to live the TDD dream!

Tuesday, July 26, 2011 3:33:25 AM (Central Daylight Time, UTC-05:00)
i enjoy your article. great job. keep it simple
delaco
Friday, March 22, 2013 9:40:08 AM (Central Standard Time, UTC-06:00)
I've always dreamed of a life of business traveling. I've already visited this wonderful country. I have at home a big collection of photos with the city and its surroundings. It's a shame that I can not upload any pictures on the site.
spence
Sunday, March 24, 2013 3:36:45 PM (Central Standard Time, UTC-06:00)
I like the motto: “Been there done that!”. It makes me think about multiple opportunities that arise in our lives and if we are bold enough we take them. Furthermore, I wish to live many more years if possible and travel as much as I can together with my family. Right now I am planning a well deserved getaway to atlanta to rest a little bit.
crina
Thursday, March 28, 2013 8:59:52 AM (Central Standard Time, UTC-06:00)
I hope to visit Chennai very soon. I choose this region as my future vacation location, but I have to search information about how much will cost me a trip here. Until now I found five restaurants to try them. My concern is the money, I always choose a cheap vacation because I want to save money for other important things. It's frustrating to need a lot of money to have a great holiday. I hope this isn't the case.
angela
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview