October 31, 2007 - 18:50 UTC - Tags: .NET BDD TDD .NetSpec testing
So I just added a new open source project to boss.bekk.no. It's called .NetSpec and the goal is the provide a
RSpec like syntax for writing tests/specifying behaviour, but still allow the tests to run through Visual Studio's test view and the TFS team build.
SyntaxThe .NET framework 3.5 contains a new feature called extension methods. This means that you can create methods that appear to be added to other objects in the language, so that when programming you can invoke a new method on an object, even though the method is not actually present on the object itself. ScottGU has a nice write-up
here.
I used the extension methods to write a small testing framework for .NET 3.5: This framework allows you to write code like this:
Stack<String> stack = new Stack<String>();
stack.push("an object");
stack.Count.ShouldBe(1);
Look at the last line of code. This is basically an assertion saying that the stack's count should now be 1. Personally I think this is much easier to read and write, than the Assert.AreEqual(1, stack.Count).
.NetSpec also includes a new way of saying that you expect a certain exception to be thrown:
Stack stack = new Stack();
Call.To(delegate { stack.Pop(); }).ShouldThrow<InvalidOperationException>();
There is also a version for the .NET 2.0 framework, but since .NET 2.0 does not support extension methods, the syntax is:
Result.Of(stack.Count).ShouldBe(1);
More informationFor more information and downloads go to:
http://boss.bekk.no/display/BOSS/.NetSpecWhat about NSpecNSpec is based upon custom attributes and a custom testrunner. I wanted a BDD framework I could easily use with the Visual Studio test runner and the TFS team build.
Suggestions, comments and contributions are highly appreciated.