Practicing BDD with python
Question:
Which are the most advanced frameworks and tools there are available for python for practicing Behavior Driven Development? Especially finding similar tools as rspec and mocha for ruby would be great.
Answers:
I am probably completely missing the point, but what I retained of the original BDD paper was that BDD was just TDD repackaged to emphasize some best practices.
If my interpretation is correct, you can get a BDD framework just by renaming methods around in any xUnit implementation. So just go ahead and use the standard library’s unittest.
EDIT: A quick google turned up a Behaviour module in the Cheese Shop. Further searching for BDD there did not find anything else.
Ian Bicking recommends using doctest for behavior driven design:
I personally tend to use nose and voidspace mock in a behavior driven design style. Specifically, the spec plugin for nose is excellent for BDD.
The Pyccuracy project is an effort to provide a domain-specific language for BDD in Python.
Unlike doctest, which works at the API level, it encodes higher-level operations such as loading a web page and submitting a form. I haven’t used it but it looks somewhat promising if that is what you’re looking for.
I like Pyccuracy a lot. I’m implementing it on a mid sized project these days.
Lettuce means to be a cucumber-like tool for python: http://lettuce.it/
You can grab the source at github.com/gabrielfalcao/lettuce
I recommend you to use a set of tools developed to help programmers in the practice of BDD and TDD. This tool set is composed by: pycukes, specloud, ludibrio and should-dsl.
Should-DSL will give you RSpec-like expectations. Everything you can do with RSpec expectation API, should-dsl does too. You can grab the latestversion from Github.
SpecLoud helps you on running BDD-like unittests. You can install it by doing
pip install specloud
Ludibrio is a library for test doubles (Mocks, Stubs and Dummies). Install it via
pip install ludibrio
And PyCukes is the main tool for BDD. It will run the Scenarios, etc. Again,
pip install pycukes
For more info please read the tools documentation at PyPi.
Try out pyspecs. Making tests easy to read and constantly running during development were two of my main goals in creating this project.
Test Code:
from pyspecs import given, when, then, and_, the, this
with given.two_operands:
a = 2
b = 3
with when.supplied_to_the_add_function:
total = a + b
with then.the_total_should_be_mathmatically_correct:
the(total).should.equal(5)
with and_.the_total_should_be_greater_than_either_operand:
the(total).should.be_greater_than(a)
the(total).should.be_greater_than(b)
with when.supplied_to_the_subtract_function:
difference = b - a
with then.the_difference_should_be_mathmatically_correct:
the(difference).should.equal(1)
Console Output:
# run_pyspecs.py
| • given two operands
| • when supplied to the add function
| • then the total should be mathmatically correct
| • and the total should be greater than either operand
| • when supplied to the subtract function
| • then the difference should be mathmatically correct
(ok) 6 passed (6 steps, 1 scenarios in 0.0002 seconds)
I really recommend behave.
Looking for a Cucumber clone for Python, I started using lettuce, but found it a pretty clumsily designed replica. Very Unpythonic.
Then I discovered behave, and have been really happy with it.
You can use “sure” for expressive assertions (just like in RSpec)
Which are the most advanced frameworks and tools there are available for python for practicing Behavior Driven Development? Especially finding similar tools as rspec and mocha for ruby would be great.
I am probably completely missing the point, but what I retained of the original BDD paper was that BDD was just TDD repackaged to emphasize some best practices.
If my interpretation is correct, you can get a BDD framework just by renaming methods around in any xUnit implementation. So just go ahead and use the standard library’s unittest.
EDIT: A quick google turned up a Behaviour module in the Cheese Shop. Further searching for BDD there did not find anything else.
Ian Bicking recommends using doctest for behavior driven design:
I personally tend to use nose and voidspace mock in a behavior driven design style. Specifically, the spec plugin for nose is excellent for BDD.
The Pyccuracy project is an effort to provide a domain-specific language for BDD in Python.
Unlike doctest, which works at the API level, it encodes higher-level operations such as loading a web page and submitting a form. I haven’t used it but it looks somewhat promising if that is what you’re looking for.
I like Pyccuracy a lot. I’m implementing it on a mid sized project these days.
Lettuce means to be a cucumber-like tool for python: http://lettuce.it/
You can grab the source at github.com/gabrielfalcao/lettuce
I recommend you to use a set of tools developed to help programmers in the practice of BDD and TDD. This tool set is composed by: pycukes, specloud, ludibrio and should-dsl.
Should-DSL will give you RSpec-like expectations. Everything you can do with RSpec expectation API, should-dsl does too. You can grab the latestversion from Github.
SpecLoud helps you on running BDD-like unittests. You can install it by doing
pip install specloud
Ludibrio is a library for test doubles (Mocks, Stubs and Dummies). Install it via
pip install ludibrio
And PyCukes is the main tool for BDD. It will run the Scenarios, etc. Again,
pip install pycukes
For more info please read the tools documentation at PyPi.
Try out pyspecs. Making tests easy to read and constantly running during development were two of my main goals in creating this project.
Test Code:
from pyspecs import given, when, then, and_, the, this
with given.two_operands:
a = 2
b = 3
with when.supplied_to_the_add_function:
total = a + b
with then.the_total_should_be_mathmatically_correct:
the(total).should.equal(5)
with and_.the_total_should_be_greater_than_either_operand:
the(total).should.be_greater_than(a)
the(total).should.be_greater_than(b)
with when.supplied_to_the_subtract_function:
difference = b - a
with then.the_difference_should_be_mathmatically_correct:
the(difference).should.equal(1)
Console Output:
# run_pyspecs.py
| • given two operands
| • when supplied to the add function
| • then the total should be mathmatically correct
| • and the total should be greater than either operand
| • when supplied to the subtract function
| • then the difference should be mathmatically correct
(ok) 6 passed (6 steps, 1 scenarios in 0.0002 seconds)
I really recommend behave.
Looking for a Cucumber clone for Python, I started using lettuce, but found it a pretty clumsily designed replica. Very Unpythonic.
Then I discovered behave, and have been really happy with it.
You can use “sure” for expressive assertions (just like in RSpec)