ISerializable

Roy Osherove's persistent thoughts
posts - 1254, comments - 2939, trackbacks - 511

My Links

News

MSDN articles
Join the TDD workshop!
This site supports RSS. why should you care?
Coding Contest: Coolest/most useful VS.Net Add-in or Macro
Join the contest and win some cool prizes!
* Contest RSS feed here
My Personal Homepage


MSN: RoyOsherove@hotmail.com
(hint: try the newsgroups first)
My Regex blog My photo blog

IL.Net - Israel's .Net user group information center
Get Feedable Today!
Get serious about regular expressions with this free tool
Book of the month: Book of the month: .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library, 1/e
More recommendations

Amazon Associate
Nedstat Basic - Free web site statistics
StatCounter - Free Web Tracker and Counter

Listed on Blogwise


Article Categories

Archives

Post Categories

About

Articles

Favorite Blogs

In My SysTray

Israeli .Net Bloggers

More FavoriteWare

Raw RSS Feeds

Regular Expressions

Sites : .Net

Sites : Misc

Sites : Unit Testing & XP

Simplified Database Unit testing using Enterprise Services

By Roy Osherove

Summary:

In this article I?ll show what problems we encounter today when we perform unit tests against database related component, and the various ways used to solve these problems. I?ll also show a new method to solve these problems, which is far easier and simpler than any of the others. At the end of this article you will have the tools necessary to do simple and easy unit tests against database related components without the need to write messy cleanup code after your tests.

 

Introduction

Test Driven Development makes life easier. Unit tests are the lifeblood of a good Test Driven Development approach (and thus, are also the lifeblood of most of the agile methodologies out there such as XP, Scrum and others). Therefore it stands to reason that one should strive to create unit tests with the least amount of work and maintenance, which will in turn allow the developer to focus on the business problem at hand and the various design and algorithms needed to create a working product/component.

 

For the past couple of years I?ve been struggling with Unit tests, That is, I?ve used them in major production projects and have seen the good and the bad that can come from a fully test Driven approach. I?ll let the ?good? part be taken care of by the numerous other articles out there that talk about the benefits you get from Unit Tests and TDD, and will focus on one of the most problematic parts of Unit tests in a real world project ? testing against the database.

 

Testing Data Access layer components (DALs for short) is problematic in a number of ways. The number one reason f is this:

 

Testing the various CRUD(Create, Retrieve, Update, Delete) operations on a single class can result in a massive amount of ?garbage? data residing on the test database (You are using a test database, aren?t you?). This creates several problems:

1.      Problem: Garbage Data

Your database will eventually be full of junk data which you?ll need to get rid of sooner or later

 

2.      Problem: Affecting other tests

Worst, you may break one of the golden rules of unit testing:  No test should be dependent on another test to perform correctly. That is, you should be able to run your tests in any order you choose or maybe just one of them at a time, and they should all still work. That means that every test needs to start with a known state on which it will act. For DALs this usually means a known state of records in the database.

But guess what? When you test CRUD operations you?re actually changing the state of the database, so the next test that will be run that might depend on a specific record being there might fail because you just deleted that record in your current test. Anything can happen.

 

3.      Problem: starting tests from a known state

This is the other side of the previous problem: My test needs to have something in the database in order to perform correctly. For example ? it needs a specific ?category? entity in the Category tables in order to test inserting a child ?Product? into the Products table. I need to make sure I insert these values into the database before I perform the test, but I also want to make sure that data is no longer there after I finish my test.

 

Dealing with the problems

So how do you deal with all this garbage data that?s making your database and tests unstable? Obviously, you need to make sure that before and after each test you leave the database in the same state that you got it in. That is ? you need to ?undo? your CRUD operations after testing them. Here are some of the most prevalent methods used to achieve this:

  1. Remove it manually

This is the most obvious and in simple cases one of the simplest to achieve. To do this, after each test (at the end of it) you execute the opposite actions than the ones you took in the test. For example, if you inserted a record in the test ? you delete it at the end.

Some problems and questions that arise from this approach:

o       If I inserted the record into the Database using my objects? Insert() method which I was testing ? should I remove it from the database using the object?s Delete() method (which might have not been tested yet or may not even exist yet!) or should I do it against the database using direct calling of stored procedures or direct ADO.Net classes?

o       If I choose to remove the records I inserted directly from the DB ? this involves some serious amounts of extra code, residing in my test! And that code can have bugs as well.

o       If I choose to use my object to delete and Insert as well - what do I test first? (Chicken and the egg kind of question..)

o       I can also choose to remove all the garbage data at the end of all the tests run (TestFixureTearDown method). But then I?d need lots of data and custom code and ?. It?s just complicated.

o       I can ignore all my garbage data and restore my database from backup before running all the unit tests. This might solve the problem partially but may still leave a few unit tests that mess with other test?s data. A big no-no.

  1. Use the transaction object in ADO.Net

In his very interesting and insightful book, ?Test Driven Development with Microsoft.Net?,  James Newkirk addresses this same problem with a seemingly simply solution: Execute all your actions inside a transaction and rollback that transaction when you?re done with each test. This is a great solution and can definitely be implemented, but also has its problems:

  • You need to worry about components that manage their own inner transaction but still will need to use outside transactions initiated by the unit tests.
  • To workaround this problem James came up with an implementation of the Command pattern inside the unit tests (the book has a good example). However ?even if it accomplishes the task nicely, after using this pattern we are left with more complicated unit tests that we should have, and all this just to be able to support rollback functionality.

 

A simpler way

The previous two approaches are the most used, and if you?ve used either of them you?d know that they are still hard to achieve with massive amount of custom work just to make the database data more reliable.

When I was reading Jame?s book, in the chapter that discusses implementing transactions, I came across this little sentence (abbreviated):

?We want to achieve the same functionality as ?RequiresTransaction? for each tested component, just like in COM+?

This sentence triggered an interesting thought : why work so hard? Why not let COM+(Or, ?Enterprise Services? in .Net) do the work of coordinating the transactions for us? Thus ? I came up with another way of implementing database rollback functionality :

 

 

Using Enterprise Services

What we want is to be able to achieve the transaction functionality without all the overhead of writing our own custom Transaction manager class as prescribed in James? book. How do we do that? With COM+ Enterprise services.  Here is the technical gist of the method:

Our simple efforts start with this simple base class


using System;

using NUnit.Framework;

using System.EnterpriseServices;

namespace TransactionTesting

{

[TestFixture]

[Transaction(TransactionOption.Required)]

public class DatabaseFixture:ServicedComponent 

{

[TearDown]

      public void TransactionTearDown()

      {

            if(ContextUtil.IsInTransaction)

            {

                  ContextUtil.SetAbort();

            }

      }

}

}


Here are some important pointers about this class:

 

  • This class will be the base class that your DAL unit tests will inherit from. It allows for automatic transaction enlistment using the COM+ architecture through the EnterpriseServices namespace in .Net.

for that end :

  • It inherits from ServicedComponent
  • It has the following attributes on it:
    1. [TestFixture]
    2. [Transaction(TransactionOption.Required)]

The second attribute specifies that whenever this class is instantiated or invoked, COM+ will automatically create a transaction context for it to run under, or enlist it into an existing transaction context.

  • It has a simple TearDown method which will call ContextUtil.SetAbort(). This causes the following flow to occur:

1.      Before each test a transaction context is opened (or enlisted into an existing transaction)

2.      The test is performed

3.      TearDown is called and the transaction is rolled back thus

 

We get the following benefits from this approach:

1.      it is dead simple and easy to implement

2.      You need no code whatsoever to roll back any database changes your components have made throughout the test. They will be rolled back automatically.

3.      You do not need to manage transaction by yourself.

 

A few more settings and we?re done

We?re still not finished but we?re awfully close to the finish line.

All serviced components need to have strong names. This one is no exception.

  • Use the SN.exe tool with the ?k option to create a key file.
  • In your test project open AssemblyInfo.cs and set the following attribute (add it if it is not there):

[assembly: AssemblyKeyFile(@"..\..\..\test.snk")] 

Notice that I?ve put here a relative file path. This is the file I generated from sn.exe and it is located in the same directory as my .sln file. That way I can put this same attribute on all the other projects that need it as well.

  • Put a static Version attribute in your test project:
    • Look for this in the AssemblyInfo.cs file :[assembly: AssemblyVersion("1.0.*")]
    • And change it into [assembly: AssemblyVersion("1.0.0")] or whatever version you like.

This step is important because by default when the attribute is at its default state Visual Stuido.Net will automatically increment the assembly version with each build. That?s a bad practice to have on any project, but when you?re dealing with a COM+  component (A ServicedComponent is just that) you need to realize that for every separate version of the test assembly that you build, you will have created a different COM+ component in the COM+ catalogue of the testing machine. SO ? after you?ve run the tests 50 times a day your COM+ catalog might turn out to look pretty scary and we don?t want that. Setting the version to a fixed version saves us this little headache.

  • Since your test assembly now has a strong name, every assembly that it references must have a strong name as well. This is easily done: put this same attribute in each of the referenced projects set in your test project.

 

That?s basically it. You?re done.

 

Here is a simple example of a test fixture that derives from this class to achieve a perfectly transparent data rollback after each test. This class tests a simple class that does category insert and delete against the Northwind database:


using System;

using NUnit.Framework;

using System.Data;

using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

using NorthwindDAL;

 

namespace TransactionTesting

{

      public class CategoryTests:DatabaseFixture

      {

            string CONN = @"Server=localhost;Database=Northwind;Trusted_Connection=True";

 

            [Test]

            public void Insert()

            {

                  CategoryManager mgr = new CategoryManager();

                  int newID = mgr.Insert("Test category");

                  VerifyRowExists(CONN, newID,true);

            }

 

            [Test]

            public void Delete()

            {

                  CategoryManager mgr = new CategoryManager();

                  int newID = mgr.Insert("Test category");

                  bool result = mgr.Delete(newID);

                  Assert.IsTrue(result);

 

                  VerifyRowExists(CONN, newID,false);

            }

 

            private void VerifyRowExists(string connectionString, int existingRowID,bool shouldExist)

            {

                  string SELECT = "Select * from Categories where categoryID=" + existingRowID.ToString();

                  SqlDataReader dr = SqlHelper.ExecuteReader(connectionString,CommandType.Text,SELECT);

                  Assert.AreEqual(shouldExist,dr.HasRows);

                  dr.Close();

            }

      }

}


 As you can see there is no clean up code anywhere. That?s how simple this technique  really is.

Just so you can see there are no tricks up my sleeve ? here is the CategoryManager tested class (in VB.Net ? just for the heck of it):


Public Class CategoryManager

    Private CONN As String = "Server=localhost;Database=Northwind;Trusted_Connection=True"

 

    Public Function Insert(ByVal CategoryName As String) As Integer

        Dim sql As String = "Insert into categories (categoryName) values('" + CategoryName + "');Select scope_identity()"

        Dim result As Integer = NorthwindDAL.Data.SqlHelper.ExecuteScalar(CONN, CommandType.Text, sql)

        Return result

    End Function

 

 

    Public Function Delete(ByVal id As Integer) As Boolean

        Dim sql As String = "delete from categories where categoryID=" + id.ToString()

        Dim result As Integer = _

            NorthwindDAL.Data.SqlHelper.ExecuteNonQuery(CONN, CommandType.Text, sql)

 

        Return (result = 1)

    End Function

End Class

 


No tricks, no hidden cards. The database actually gets updated, only the updates are rolled back.

 

Conclusion

In this article I?ve show a way to define simpler, more maintainable unit tests against the database access layer components using the powerful and easy to use services of COM+.  Hopefully using this technique database unit testing can become a much less painful experience, and thus allow more developers do more tests on their database code ? and find those bugs as soon as possible.

 

posted on Saturday, June 19, 2004 10:51 PM

Feedback

# re: Simplified Database Unit testing using Enterprise Services

Very nice!
6/20/2004 12:02 AM | Addy Santo

# re: Simplified Database Unit testing using Enterprise Services

This is awesome Roy! Great use of ES, very insightful idea. I am a big fan of ES and we (IDesign) recommend its use to many of our customers. Now I have a great way to show them how it can help their TDD as well (with all due credit to the originator of the idea).

Scha-weeet.
6/20/2004 1:41 AM | Brian Noyes

# re: Simplified Database Unit testing using Enterprise Services

Nice!
6/20/2004 3:36 AM | Steve

# re: Simplified Database Unit testing using Enterprise Services

Excellent. Alexei and I were troubled by the TransactionManager class in the book and we thought there must be a better way. Thanks for pointing this out and for the great article.
6/20/2004 3:58 AM | James Newkirk

# re: Simplified Database Unit testing using Enterprise Services

Nice article, Roy! I'm one of those that think Enterprise Services doesn't get enough attention, and hopefully this helps bring it a little more to the forefront.
6/20/2004 5:01 AM | Dave Donaldson

# re: Simplified Database Unit testing using Enterprise Services

Thanks folks. I'm flattered that you like it.
I'm still trying to find holes in my theory and "bugs" in it.
Please let me know if you find any of those. I'll update the article accordingly.

Thanks,
Roy
6/20/2004 9:20 AM | Roy Osherove

# re: Simplified Database Unit testing using Enterprise Services

Simple and effective

This is a great solution Roy, I wonder if that can also be used against LDAP, i'll check it up...
6/20/2004 9:57 AM | Ido Samuelson

# re: Simplified Database Unit testing using Enterprise Services

Very Nice Roy !!
(I was just wondering about the subject the other day...)
A few questions:
1. Can i use the same method for testing my BL / WF layers ?
or is there a better test method for those layers...
2. Does it matter if my DAL is ES or not ?
3. you mentioned that "every assembly that it references must have a strong name as well", does it means my DAL needs a strong name ?
(if its ES then of course it has, but what if its not ?...)
Thanks,
Ido.
6/20/2004 11:50 AM | Ido Tandy

# re: Simplified Database Unit testing using Enterprise Services

Very nice indeed. I think that it's a fantastic way to do DB testing.

One small aside, though - in your categoryManager code, your building sql strings dynamically which opens up the possibility of sql injection attacks. You should be using parameterized sql - either in sprocs or in your own code.

But still, EXCELLENT article.
6/20/2004 11:51 AM | Udi Dahan - The Software Simplist

# re: Simplified Database Unit testing using Enterprise Services

Hi, Roi.

Great article, very insightful.

It definitely gives a good solution to most cases, however, there are a few exceptions:

1. If your ADO.NET provider does not support distributed transactions (most popular RDBMSs support this but not all of them) - for example MSACCESS or an early version of IBM DB2.

2. If the component you are testing is using Enterprise services, but with inappropriate Transaction attribute ("Disabled", "Not Supported" or "Requires New").

3. If the component is performing an action that cannot participate in a distributed transaction (such as a DDL command, that commits automatically for most RDBMSs).

For these exceptions, I don't see any other way but to resort to other methods. There's no quick fix for using Enterprise Services (unless you're willing to change your tested component).

Another thought: How about using MS DTS? You can easily create a package that generates your initial database state on a new schema, and than run the package before the beginning of the test.

Best Regards,
Amir.
6/20/2004 11:57 AM | Amir Tuval

# re: Simplified Database Unit testing using Enterprise Services

Ido:
1. yes
2.No, unless the DAL has explicit "Don't use transactions" attributes.
3.yes

Udi:
It was for the same of example. I wanted to show there is no behind the scenes goo going on. Obviously that measn not production ready code.

Amir: Thanks for the insightful comments.
1. agreed.
2. agreed.
you could solve that by doing #ifdefs at the component level for DEBUG and RELEASE modes I would guess.

3.Didn't consider that, good point.

The main thing is that this method solves the pain problem for *most* cases out there. to me, solving 80% of the cases is much better than nothing. Obviously this isn't a silver bullett. Nothing ever is.
6/20/2004 12:14 PM | Roy Osherove

# Simplified Database Unit testing using Enterprise Services (by Roy Osherove)

6/20/2004 5:28 AM | Peli's Blog

# re: Simplified Database Unit testing using Enterprise Services

Perfect timing as I'm just beginning my foray into ES :-) Two birds with one stone, good stuff Roy!
6/20/2004 1:14 PM | senkwe

# Unit Testing in .NET - NUnit - Automating Unit Tests - Testing a Database

# re: Simplified Database Unit testing using Enterprise Services

It's not exactly true that the state of the database is the same if you rollback the transaction. If you have IDENTITY fields, then, even if you did not commit the transaction, the values you got are gone, and the next time you'll get different values.

So, you need to be very careful with foreign keys that map to autonumbered primary keys, as you cannot rely in the values you get. If you need a CategoryId in a Customer table, then you need to first insert a Category, get the identity value, and use it in the Customer table.

When the database is complex (e.g., to insert a record in 'Customer' you first need to insert a record in 15 tables), this can make your unit tests very complex.

Creating the database from scratch is a better solution. In that case you can assume that the first CustomerId you'll get is 1, and hard code that '1' in your tests. Is less elegant but easier.

6/20/2004 6:51 PM | Andres Aguiar

# re: Simplified Database Unit testing using Enterprise Services

Andres: You have a point, btu I'd still rather do those inserts rather than recreate the DB from scratch before every test, which I think would big performance consequesnces.
6/20/2004 8:24 PM | Roy Osherove

# re: Simplified Database Unit testing using Enterprise Services

Yes, that's also true.
6/21/2004 2:37 AM | Andres Aguiar

# Database

6/20/2004 8:00 PM | Peli's Blog

# Database

6/20/2004 8:01 PM | Peli's Blog

# re: Simplified Database Unit testing using Enterprise Services

Pretty. Simple.
Thanks!


6/21/2004 5:50 PM | Justin Pitts

# re: Simplified Database Unit testing using Enterprise Services

Roy -- interesting article.

We were doing this 8 months ago on our own team (though, now I wished we had written about it then! ;) ).

We found issues, though, when testing with Server Application components. Also, we found issues with the various Oracle providers (both from Microsoft and Oracle) not playing well with NUnit started and ended transactions. With Server Applications, in particular, if you need the client to call Dispose explicitly, and in this case, the client is NUnit, which is also not derived from ServicedComponent.

I will write more about these issues and problems to watch out for on my blog later on today.
6/21/2004 6:44 PM | Robert Hurlbut

# re: Simplified Database Unit testing using Enterprise Services

Robert - Excellent!
6/21/2004 6:54 PM | Roy Osherove

# Enterprise Services and TDD

6/21/2004 12:02 PM | Robert Hurlbut's .Net Blog

# re: Simplified Database Unit testing using Enterprise Services

Great article Roy. I just managed to set up something akin to James's approach in my app, and now I'll be changing it again...

As an aside, you replied to the question about the DAL saying it would have to be signed. Am I missing something here, or could you avoid this by putting the Serviced Component test base class in a strong-named assembly, and referencing it from your main test assembly/ies? It doesn't appear to me that your ServicedComponent should have to know about your DAL. Then again, I may be all wet here!

Dave
6/21/2004 7:41 PM | Dave Hallett

# re: Simplified Database Unit testing using Enterprise Services

Dave: no, that would not work (even if it sounds like it).
Because your derived text fixutres inherit from a ServicedComponent, they themselves are servicedcomponents and thus the assembly they reside in should also have a strong name. Which leads down the same path outlined in the article.
But thanks for asking. I actually tried before answering because I wan't sure myself.
6/21/2004 11:39 PM | Roy Osherove

# re: Simplified Database Unit testing using Enterprise Services

Thanks Roy. It sounded plausible, but I had a feeling it would be too easy...
6/22/2004 1:32 AM | Dave Hallett

# Enterprise Services and TDD

6/21/2004 7:48 PM | Robert Hurlbut's .Net Blog

# re: Simplified Database Unit testing using Enterprise Services

Roy -- I ran a test today with some simple code against an Oracle database using your method. I tested against a Server Application component. Follow the link above to see my results.
6/22/2004 2:50 AM | Robert Hurlbut

# Enterprise Services and TDD part 2

6/22/2004 5:34 AM | Robert Hurlbut's .Net Blog

# Enterprise Services and TDD part 2

6/22/2004 6:05 AM | Robert Hurlbut's .Net Blog

# re: Simplified Database Unit testing using Enterprise Services

"Am I missing something here, or could you avoid this by putting the Serviced Component test base class in a strong-named assembly, and referencing it from your main test assembly/ies?"

Unless I'm incorrectly interpreting your question, the application of the AllowPartiallyTrustedCallersAttribute class would more than handle a situation like that.
6/23/2004 5:03 AM | Derek Simon

# re: Simplified Database Unit testing using Enterprise Services

Hi Roy,

It realy looks like a simple & effective approach that can help for 80% of the cases.
I thought on some problem that maybe related not just to this article but to the whole TDD approach (I admit that i'm not very familiar with this approach):

The tests results are based on the ability to verify automatically the results of the actions done during the tests (you used the "VerifyRowExists" method in your example). I support the need for automatic verification of the tests and in your case there is no other way because the transaction is automatically rolled back at the end of the test, but when you are writting code to verify test results some bugs in the verification process are eventually inevitable.

In your example the verification process is quite simple and you can write a generic code that do the "VerifyRowExists" logic and test it once seperately and by that you are solving most of the problem, but sometimes verification code is much more complex (e.g. when the dal operation is not running a simple CRUD function but rather handles multiple records by running a stored procedure or by triggers invoked automatically by the database). For instance, you might design your system in a way that when you delete a category that contains some items, all of the items should not be deleted, but rather shipped to a default category.

The big problem might occur when the bug in the verification does not fail the test but rather approves a test that has a bug.

Regards,
Omer.
6/23/2004 10:47 AM | Omer Shwartz

# re: Simplified Database Unit testing using Enterprise Services

Hi!. Very nice solution.
I have exact database context decribed here in this post. I use this technique, it works well on nunit-console, but it didn't work on nunit-gui. Nunit-gui constantly crash and the tests do not run. I?m trying to see what is happening.


Regards,
Rui
6/23/2004 2:26 PM | Rui Rosa Mendes

Post Comment

Title
Name
Url
Comment 
Privacy | Terms of use