Specter Ghost Logo

Specter

Introduction

Specter is an object-behaviour specification framework. It enables behaviour driven development by requiring developers to write executable specifications for their objects, before actually implementing them. Technologically this is similar to test driven development, however the shift in nomenclature removes the psychological barrier of writing "tests" for code that does not exist. (Existing projects implementing this idea include RSpec for Ruby and NSpec for .NET.)

SourceForge project page here: http://sourceforge.net/projects/specter

Specter targets the .NET CLI by creating specifications using the Boo programming language. The use of Boo's syntactic macro ability and extension methods allows for almost natural language specs, as seen in this example:

import Specter
import NUnit.Framework 
# a context is effectively a class that specifies the
# behaviour of an object that is in a given state.
context "Empty stack":
    # object the spec is defining
    stack as Stack
    
    # optional setup will be run before executing each specify block
    setup:
        stack = Stack()
    
    # specify can just be a single line
    specify stack.Count.Must == 0
    
    # Or it can a block of statements
    specify "Stack must accept an item and count is then one":
        stack.Push(42)
        stack.Count.Must == 1
    
    # You can specify that a block of code must
    # throw an exception when executed.
    specify { stack.Pop() }.Must.Throw(typeof(StackUnderflowException))
    
    # optional teardown is called after each specify
    # perform clean up actions here.
    teardown:
        stack.Dispose()

The spec is expanded by syntactic macros (context, setup, specify, teardown) into a standard .NET class at compile time. NUnit attributes applied to the class and methods allow it to be "executed" within NUnit, just like any other test fixture. Therefore, you can reuse all your existing continuous integration tools. Go to the NUnit Integration page for more information.

The departure from using explicit classes and methods with attributes (like normal NUnit code) reduces the spec down to an almost pure form – effectively a simple domain specific language for specification.

Compare:

[Test]
public void StackCountMustEqual42()
{
    Assert.AreEqual(42, stack.Count)
}

with just,

specify stack.Count.Must == 42

The specify macro can also extract a human readable string from the code, describing the specification! This means error messages reported by NUnit make sense to read, without having to write them yourself.

A key part of Specter is the Must extension method available on all .NET types. Must provides access to many different assertions you can make about your objects. Go to the Assertion Features page to see what assertions Specter provides.

Writing the spec in Boo leaves you free to write your application code in any .NET language. This means you can write the spec with Specter and have it run against a C# or VB.NET application implementation, for example.

Copyright © Andrew Davey 2006
Check my blog for news and updates.

SourceForge.net Logo