Category Archive

The following is a list of all entries from the Testing category.

Eiffel-style Code Contracts for .NET

Design-by-Contract was coined by Bertrand Meyer, the creator of Eiffel as a way for software designers to formally define pre and post conditions as well as object invariants such that the caller can know what to expect when executing code.  

This practice is applied in unit testing when Asserting the result of a function, however until now there have only been work arounds for achieving the same logic in-line; which can now be elegantly expressed like using Code Contracts like so:

public void Deposit(int amount)
    Contract.Requires(amount > 0);
    Contract.Ensures(Balance == Contract.OldValue(Balance) + amount);

    Balance += amount;

Microsoft through its DevLab site has released Code Contracts for .NET, a subset of its ongoing Spec# research project.  This consists offering consist of:

  • a stand-alone library that will in future ship as part of the BCL in .NET 4, 
  • a binary rewriter to support post-condition checking, 
  • and extensions to the Visual Studio 2008 IDE to support static checking after every compile.   

As part of the distribution it includes a number of helpful code snippets such as:

cr Contract.Requires(…);
ce Contract.Ensures(…);
ci Contract.Invariant(…);

Where the Contract.Invariant call is declared within a protected ObjectInvariant method (snippet cim) and defines the state of a property throughout it’s life cycle: 

protected void ObjectInvariant ()
    Contract.Invariant ( f ! = null );

I recommend watching the short channel-9 video for an introduction, or consult the manual for further details.  One feature which is not currently available will be integration with code documentation, which will be very helpful for API developers.


Billy McCaferty and his S#arp Architecture

I have been following Billy McCaferty’s Sharp Architecture project for some time now – a best practice Domain Driven Design approach to developing ASP.NET MVC web apps.  It has seen a number of iterations, the latest including the configuration with FluentNhibernate and independent ServiceLocator

In his recent article on InfoQ he discusses some of the motivations behind his TDD/DDD implementation.

What’s currently lacking, at least in the world of .NET web development, is a common architecture and foundation for application development which combines best of breed technologies and techniques, using recent technologies based on proven practices, while taking into account the availability of high quality tools developed by the open source community. S#arp Architecture is a response to this need. The open source S#arp Architecture attempts to leverage the proven practices described in this article with carefully selected tools to increase development productivity, while enabling a high level of quality and maintainability

He also makes dudious use of the T4 templating support built into VS2008 to codegen files from the Model objects through to Views and Controllers.  A great kick start for any new project – Well worth a read!

Regression testing with PEX

A new project from Microsoft research, PEX is an smart tool integrated directly into Visual Studio which helps you generate test suites with max code coverage.  It analyzes your code and suggests various arguments to ensure all paths of your code are tests.  These arguments and return results can then be turned directly in to .NET test methods either using the standard VS Test framework, or NUnit/MBUnit via extensions.

Also recently released is another free testing tool NP .NET Profiler, which can be hooked into .NET processes such as IIS to monitor memory and performance bottlenecks as well as catch first chance exceptions.  This low impact tool can be used in production environments and even has its own query interface for getting to the heart of problems.