Project Description
Cspec is a testing framework inspired by ruby RSpec, and more behavioral driven approach.

The main goal of this project is to have a very fluent testing framework, that can be expressive and replicate the real language as close as it can.

It uses a combination of extension methods, lambda parsing and custom delegates to replicate fluent language and behavioral pattern.

[We are also on GitHub]

Simple Example:

public class MyClass
public int Sum(int a, int b)
return a + b;

public int Sub(int a, int b)
return a - b;


All we  need to do now is to describe our dummy class like so:

//This is our Spec class that describes operations on the object.
public class MyClassSpec : CSpecFacade<MyClass>
public MyClassSpec()
: base(new MyClass())
Should(@have => ObjSpec.Total == 0);

[It("Sums two numbers, given 1 and 1 it should be 2")]
Describe describe_sum_at =
(@do) =>
@do.Sum(1, 1).Should(@be => 2);

DescribeAll describe_sum =
(@it, @do) =>
@it("Sums two numbers, given 1 and 1 it should be 2");
@do.Sum(1, 1).Should(@be => 2);

DescribeAll describe_sub =
(@it, @do) =>
@it("Substracts two numbers, given 1 and 3 it should be -2");
@do.Sub(1, 3).Should(@be => -2);

The tags in the Should method are special tags that tell the framework how it should react, @be tells to check if the return value is equal to the right hand side.

The @have tag is used as a null check if the argument is a reference or value type (it will be boxed), but if it is a boolean it will assert if it is true.

[There are more tags that can be used like: @be, @not_be, @have, @contains and more will come]

The @it, and @be tags are just naming convention so you don't need to use those names.

@it describes what the operation is going to be about.

@be is the class that's being used to do operations.

For more info on how to use look up the code, and the tests, where CSpec is tested with CSpec.

CSpec uses two types of descriptor delegates DescribeAll and Descibe where you can use the ItAttribute to write the operation description,

this was done mainly because the feedback showed that there are people that like the attribute tag more then the @it delegate call.

Now to run it simple use a CSpecTestRunner class or use the shell interface to call testing application like so:

CSpec.Console.exe runner MyAssemblyContainingSpec.dll -a

One important detail is that the path to the assembly must be absolute.

-a is used to display detailed console information.

For more information type help to get a full list of commands and attributes/

CSpec.Console.exe help


Another Example:

Now let's compare codes from CSpec and RSpec

The code taken from RSpec site.


# bowling_spec.rb
require 'bowling'

describe Bowling, "#score" do
it "returns 0 for all gutter game" do
bowling =
20.times { bowling.hit(0) }
bowling.score.should == 0


The same code in CSpec would look like so:


public class BowlingSpec: CSpecFacade<Bowling>
public BowlingSpec()
: base(new BowlingSpec())
Should(@have => ObjSpec.Score == 0); //not needed

DescribeAll describe_bowling_score =
(@it, @do) =>
@it("returns 0 for all gutter game");
int game = 20;
game.Times(()=>{ @do.hit(0) });
@do.Should(@be => 0);


For more examples and information visit the Documentation page, and check the Sandcastle class documentation.


Project Status:

As of now the project has a very small subset of features, (I coded it in about three days of my free time)

They include:

  • Uses the behavioral fluent approach, to test code making it possible to think of a problem in a higher abstraction level.
  • CSpecFacades and StaticFacades for static and non static types.
  • Four lambda tags for the Should methods making it possible to describe code.
  • Simple test runner for testing but it can be also used as a method and type Guard.

What's next to come:

  • Hack C# or do whatever it takes to make this a more fluent experience (this includes writing a DLS that is static typed).
  • Create a code comments and documentation in 3:1 ratio of the code base (so far we at best have 1:2 ratio).
  • Add new validation extensions and more lambda tags and in general more features.
  • Add the ability to customize console for the shell and also the ability to create high level report docs.

Have fun :-)

ATENTION: there is a bug in C# compiler making some facades to fail when they dont have an Action<string> delegate in the Spec class,

this field does not need to do anything, without it it's sometimes impossible to determine the delegate type in base class.

By contrast the code does not need this dummy deletage in Mono :-)

Update: This is indeed a bug on the MS side

for more information look in \src\CSpec.CSpecTest\Test.Spec\CSpecTestRunnerSpec.cs


  • Project Supporters are very welcome, as well as requests and feedback. 
  • If you are a passionate about what you do and want to say Hi please do so, I would like to meet you :-)
  • Some info about me. blog and my web site

Last edited Jun 19, 2011 at 12:41 AM by online55, version 30