27 days after its first release candidate, lettuce is now much mature and also got new features.
Lettuce is a BDD tool written in python, 100% based on cucumber.
There are two reasons for the name:
- Lettuce is a green vegetable, just like cucumber the idea is that your tests must be always green.
- Letuce is the name of a brazillian band that I pretty much like
Hands on!
There are links to lettuce’s official documentation at the end of this post, but there is a sneak peak:
Features are described in files with .feature extension and must be, by defaul within your project, in a folder called “features“.
Features are described like this:
In order to show it working
As lettuce author
I want to create a scenario that passes
Scenario: Concatenate names
Given I have the following names:
| name | surname |
| john | doe |
| ian | murdock |
When I join them
Then I see the data:
| joined |
| Doe, John |
| Murdock, Ian |
In order to define steps all you need to do is write a new python file wihin features folder, Lettuce will import them all, recursively.
Example:
from lettuce import step, world, before
from some_module import NameJoiner
# setup
@before.each_scenario
def set_joined(scenario):
world.joined = []
# steps
@step('I have the following names')
def set_names(step):
world.people = step.hashes
@step('join them')
def join_names(step):
for person in world.people:
joiner = NameJoiner(
name=person["name"],
surname=person["surname"]
)
world.joined.append(joiner.join())
@step('I see the data')
def check_names(step):
for name, data in zip(world.joined, step.hashes):
assert name == data['joined']
(the full example is available here aqui)
Motivation
Since I started with TDD in Python I had many experiences, I’ve tested many mock mock libraries and test techniques.
In web projects I’ve used Pyccuracy, so that I could both describe behaviour of the application and make automated acceptance tests.
Althrough, I had a chance to try Cucumber with Django here at globo.com, and the result was: I fell in love with it, and had incredibly productive features, such as step tables which allows drawing a ascii table and map it into a list of hashes, and the scenario outlines.
From there I had THE inspiration: to implement a tool that works exactly like Cucumber, but in python.
Why ?
Python programmers it’s easier and frictionless to use libraries written in Python. I personally avoid mixing programming languages in a single project, some technologies can suit perfectly in the project, no matter in what language, but if there is a Python option, I will give it a go.
Cucumber IMHO one lib to make ruby even more “sexy” ![]()
Build a robust application, well tested required patience, what about turn it into fun ! ?
It’s easier to package python modules for debian/ubuntu.
Besides, even considering the fact that Cucumber already supports, would be more interesting to user pure python within step definitions.
Supposing that a web application will be written with Django and Cucumber, would be very useful to manipulate models within step definitions. However turning it possible, would need efforts to run rubypython, which the latest release dates october 2009.
Examples of similar libraries between python and ruby
Web framework:
ruby: rails python: django
Micro web framework:
ruby: sinatra python: cherrypy
Automated deploy:
ruby: capistrano python: fabric
Behaviour-driven development:
ruby: cucumber python: lettuce ?! (maybe ?! Who knows ?!)
More on lettuce
Documentation
Available at http://lettuce.it covers all supported features until now.
Contribute!
I’ll appreciate all kinds of feedback, to do so we have some channels:
Lettuce is under GNU GPL 3+ and have a long way until get robust, and any kind of help is welcome: patches, documentation, language support and so on!
Grab it, and happy hacking


