Aloe: BDD testing via nose¶
Aloe is a Gherkin-based Behavior Driven Development tool for Python based on Nose.
Getting Started¶
Install Aloe:
pip install aloe
Write your first feature features/calculator.feature
:
Feature: Add up numbers
As a mathematically challenged user
I want to add numbers
So that I know the total
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 30 into the calculator
When I press add
Then the result should be 80 on the screen
Features are written using the Gherkin syntax.
Now run aloe features/calculator.feature
and see it fail because there are
no step definitions:
$ aloe features/calculator.feature
(...)
aloe.exceptions.NoDefinitionFound: The step r"Given I have entered 50 into the
calculator" is not defined
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
Now add the definitions in features/__init__.py
:
from calculator import add
from aloe import before, step, world
@before.each_example
def clear(*args):
"""Reset the calculator state before each scenario."""
world.numbers = []
world.result = 0
@step(r'I have entered (\d+) into the calculator')
def enter_number(self, number):
world.numbers.append(float(number))
@step(r'I press add')
def press_add(self):
world.result = add(*world.numbers)
@step(r'The result should be (\d+) on the screen')
def assert_result(self, result):
assert world.result == float(result)
And the implementation stub in calculator.py
:
def add(*numbers):
return 0
Aloe will tell you that there is an error, including the location of the failing step, as if it was a normal Python test:
$ aloe features/calculator.feature
F
======================================================================
FAIL: Add two numbers (features.calculator: Add up numbers)
----------------------------------------------------------------------
Traceback (most recent call last):
(...)
File ".../features/calculator.feature", line 11, in Add two numbers
Then the result should be 80 on the screen
File ".../aloe/registry.py", line 161, in wrapped
return function(*args, **kwargs)
File ".../features/__init__.py", line 25, in assert_result
assert world.result == float(result)
AssertionError
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Let’s implement the function properly:
def add(*numbers):
return sum(numbers)
Now it works:
$ aloe features/calculator.feature
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
History¶
Aloe originally started life as a branch of the Python BDD tool Lettuce. Like so many succulents, it grew into so much more than that.