Unit Testing and Test Spy Library Released!

May 20, 2009 21:12 by garrymc

Its been some time since my last post, so I thought I’d start back with news of a small Unit Testing library I’ve been using with my current client. I developed the library to solve a number of issues I was facing while doing my unit tests. The focus of the library is to provide support for:

  • Test Spy Pattern: a type of of fake that monitors calls and data passed rather than trying to mock the implementation. A discussion of the pattern can be found in the xUnit Patterns book.
  • Comparing entities: the amount of code generally required to test that an entities properties match those of the expected version can be large and tedious to write.
  • Data access testing: while using Fakes and the like is great, you still need to test the code that actually talks to the database. This usually ends up taking a reasonable amount of code to setup and execute, making the test harder to read.

This library provides solutions to the above problems which in my experience has produced cleaner tests that are easier to read and understand. The library comes with a fairly detailed User Guide so I wont go into the details of how everything works here, however I will provide a quick overview of how the library attempts to solve these issues.

  • Test Spy pattern: adding a single property per method that you want to track to your existing Fake (Test Double) and one or two lines of code to the Fakes method implementation provides the following features:
    • Was called
    • Method call count
    • Parameters passed in to method (per call)
    • Ability to define the data returned from any method of a dependency
    • Ability to simulate an exception in a dependency
  • Comparing entities: given two object graphs you can compare each property in a single line of code using the Compare method. The library also supports the ability to ignore certain properties that tend to always change such as primary keys that are identity fields. The Compare method currently handles the basic value types and strings as well as more complex types such as XmlDocument and DataTable, adding a new complex type is a achievable by simply defining a new Type rule.
  • Data access testing: when testing code that affects a database, you need to ensure the database is in a particular state before starting. This can be done via calling a stored procedure or maybe running a database script. The library opts for the second approach and allows you to embed SQL script files which can then be executed with a single line of code. This approach ensures devs always have the latest scripts and dont have to remember to update their local copy of the database. There are a other methods that help with querying the database after executing the code under test too.

The library also provides helper routines around Serialization and IO. You can download the library and its User Guide from the CodePlex project below:

http://www.codeplex.com/UnitTestEx

As I find the time I’ll blog about some of the technical aspects of the library such as extending the Compare method it to support new complex data types.

If you end up using it in your project, please leave a comment or if you have any feature improvements (or bugs) post them to the CodePlex site.

kick it on DotNetKicks.com
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 2.0 by 4 people

  • Currently 2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments