Derik Whittaker



Code Contracts Primer – Part 3: Providing Support for Unit Tests

If you are not familiar with the Code Contracts library which is coming out of Microsoft R&D labs, you need to check this pretty cool little library out.  As of Vs2010/.Net 4.0 this library will be making the jump out of the R&D labs. 

Over the next few blog posts we will be taking a look at the topics below.

In this post we are going to take a look at how you can setup your unit tests to handle violations from your contracts.  If you do not provide any additional support in your code and you violate a contract at test time you will see a dialog like the one below:


The problem with having a dialog like this appear during your test runs should be apparent.  Any type of dialog will cause any automated build/test system to pause and wait until there is some user interaction, this is BAD.

The good news is that resolving this issue is actually simple and can be done with a few lines of code.  All we need to do is hookup the event listener which the contracts library provides and trap for the violation.  The code below shows us how to do this.

private bool _contractViolationHandled;

public void Setup()
    Contract.ContractFailed += (sender, o) =>
                                       _contractViolationHandled = true;

public void TearDown()
    _contractViolationHandled = false;

As you can see from the code above, wiring to handle the violations is simple.  But we should point out a few lines to pay attention to.

  1. o.SetHandled(); – If you do not set this you will still be able to handle the error, but it will continue to bubble up and you will still get the dialog
  2. [TearDown] – because we are setting a global variable in the setup, we MUST rest that value other wise you may get false errors in future test runs.

There you go, if you need to trap hand handle contract violations in your unit tests, you can utilize the technique above to avoid getting the violations dialog.

Till next time,

Posted 06-13-2009 9:46 AM by Derik Whittaker
Filed under: ,



DotNetShoutout wrote Code Contracts Primer – Part 3: Providing Support for Unit Tests - Derik Whittaker -
on 06-13-2009 11:28 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Bill Sorensen wrote re: Code Contracts Primer – Part 3: Providing Support for Unit Tests
on 06-16-2009 1:27 PM

Assuming Contract is a Singleton, isn't that adding an additional event handler every time Setup is executed (once for every test method)?

Derik Whittaker wrote re: Code Contracts Primer – Part 3: Providing Support for Unit Tests
on 06-16-2009 4:31 PM


What i did not put in the demo code was the call to unwire the event in the TearDown.  This was an oversight on my part.

sakthi wrote re: Code Contracts Primer – Part 3: Providing Support for Unit Tests
on 07-03-2009 8:49 AM

Hi really a nice post.... Those illustrative images and clear code had added plus for your Article..... The points you gave in handling the violations is well explained. I am also doing a project in unit tests to handle violations for macrotesting. I had posted some articles related to this in Thank you for this nice post this makes a bell ring in my mind ...



About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)