Friday 20 February 2009

Upcoming features - some thoughts

I've got some ideas in mind for the next release of lancelot:
  • Provide a bridge (in both directions) between unittest and lancelot, so that lancelot specs can be run as if they were TestCase-s, and unit tests can be verified in lancelot.
  • Allow documentation to be generated from specs as they are run.
  • Add some syntactic sugar to improve the readability of "given/when/then..." statements
  • Introduce a spec mutator similar to Pester (and Jester), to ensure confidence in verified specs

I won't have as much free time to work on this next release so I doubt whether more than one of these will make it into the codebase over the next 4 weeks... we'll see! :-)

Lancelot 1.0RC2 available

Python3 and 2.5 packages are available at the Python Cheese Shop Here are the release details, taken from the launchpad release summary:

This release is intended to be the last before the final 1.0 release. It includes:
  • a Spec class to allow BDD-style specifications (given, when, then...) to be made about the behaviour of an object instance or standalone-function (e.g. should_be, should_contain, should_raise, should_collaborate_with)
  • a MockSpec class to allow collaborations with other object instances to be specified
  • an @verifiable function decorator to allow specifications to be verified
  • an @grouping class decorator to allow specifications to be logically grouped together
  • a verify() function to use when verifying one or all verifiable specifications
  • a number of Comparable classes to use in specifications
  • example code to illustrate the various features
  • package, class and method docstrings
  • a comprehensive set of specifications for the specification classes themselves


Changes made since rc1 are generally minor:
  • new MockResult class has been added, to clarify will_return() behaviour and allow will_raise() statements
  • new @grouping class decorator has been added, to allow @verifiable specifications to be logically grouped together
  • fail_fast option has been added to verify() function
  • it() method has been added to Spec class
  • there's been a general tidying-up of code, docstrings and specs to keep pylint score above 9.0 and pycoverage at 99%

Wednesday 18 February 2009

A couple of Specs

Here are a couple of snippets of sample code for lancelot.

First up, a standalone function:
import lancelot

def fib(ordinal=0):
    '''Simple fibonacci generator'''
    ...

@lancelot.verifiable
def specify_fib_zero_to_five():
    '''First five fibonacci numbers should be 1,2,3,5,8'''
    spec = lancelot.Spec(fib)
    spec.fib(1).should_be(1)
    spec.fib(2).should_be(2)
    spec.fib(3).should_be(3)
    spec.fib(4).should_be(5)
    spec.fib(5).should_be(8)


Next, a standalone class:
import lancelot

class Stack:
    '''A simple stack with push, pop and peek'''
    ...

@lancelot.verifiable
def behavior_of_stack_with_values():
    '''Should be able to peek() and pop() values after push()'''
    stack = lancelot.Spec(Stack, given=new_stack)
    stack.when(stack.push(value='a'))
    stack.then(stack.peek()).should_be('a')
    stack.then(stack.pop()).should_be('a')
    stack.then(stack.peek()).should_raise(IndexError)
    stack.then(stack.pop()).should_raise(IndexError)


Finally, a class whose behaviour involves collaborations with other classes:
import lancelot

class Observable:
    '''Simple class that sends notifications to its observers'''
    ...

@lancelot.verifiable
def observable_observer_behaviour():
    '''Added Observers should receive Observer notifications.'''
    observer = lancelot.MockSpec()
    observable = Observable()
    spec = lancelot.Spec(observable)
    spec.when(spec.add_observer(observer))
    spec.then(spec.send_notification())
    spec.should_collaborate_with(observer.notify(observable))

Tuesday 17 February 2009

Introducing... Lancelot

I've been working on lancelot: a specification and verification library for python, inspired by the particular idiom of test driven development that goes under the banner of BDD. (And the word idiom is a film reference: Sir Lancelot: "Um, I think when I'm in this idiom, I sometimes get a bit, uh, sort of carried away.")

The code is available in a launchpad bazaar repository at http://launchpad.net/lancelot, and the packages can be downloaded from the Cheese Shop at http://pypi.python.org/pypi/lancelot. Versions for both Python 3.x and 2.5 are available: if you check them out please let me know what you think!

PS: Although I'm aware of PySpec and SpeciPy, I haven't actually used them, so I can't really compare lancelot with what they offer. My motivation for starting a new project was that I wanted something to use with Python 3, and I thought I could put my long experience with TDD to good use. (Perhaps I should qualify "long experience"... I've been using JUnit since 2000, was a committer on the Java XmlUnit project, and in 2003-4 I had the pleasure of working alongside Steve Purcell, the author of the Python unittest package, and Nat Pryce, Steve Freeman and Joe Walnes, the authors of the JMock framework. More recently, after some discussion with Andrew Glover, I have been experimenting with easyb, the Java/Groovy BDD framework).

Origins

The title of the blog refers to a scene from one of the Monty Python films. Because, sometimes, practicing test-driven development in Python (compared to say Java or Ruby) does feel a little like being asked to cut down a mighty tree with a fish...