134

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.

the_drow
  • 18,571
  • 25
  • 126
  • 193
JtR
  • 20,568
  • 17
  • 46
  • 60

10 Answers10

48

Lettuce means to be a cucumber-like tool for python: http://lettuce.it/

You can grab the source at github.com/gabrielfalcao/lettuce

user333958
  • 497
  • 1
  • 4
  • 2
  • windows users considering lettuce should know, at the time of writing, support for that OS is not straightforward. – leonigmig Mar 06 '11 at 13:41
  • 7
    Any users intending to use lettuce with django should be aware that by default, it uses your **default** database for testing. This interesting design choice cost me one production database :( – Rachel Sep 26 '12 at 07:49
  • 3
    There are some alternatives to Lettuce as well, such as Behave; here's [a blog post comparing them, and advocating Behave](https://fortylines.com/blog/LettuceVsBehave.blog.html). – floer32 Feb 05 '13 at 18:10
  • 1
    Thanks @seafangs - Behave looks much manageable for large projects than Lettuce. – jamesc May 13 '13 at 21:44
  • If you are using django, save yourself some time from using Lettuce, the current version 2.19 doesn't work with latest django. – James Lin May 06 '14 at 03:10
46

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.

Gregarious
  • 586
  • 4
  • 9
  • 11
    I switched to behave from lettuce when its default behaviour of using the default database for testing in a django project cost me a production database on a live server :( I really like behave; I've started the django-behave project to hook it into django's test framework https://github.com/rwillmer/django-behave – Rachel Sep 26 '12 at 07:51
  • 1
    I feel your pain, also I'm glad to see your suffering has contributed to the thriving of django ecosystem. ;-) – John Wang Oct 10 '14 at 10:14
  • 1
    Can I use behave without the feature files? I don't have non-technical users so writing them is just noise to me. If someone can't read my given/when/then tests they have no business in there any way. – jeremyjjbrown Nov 07 '14 at 02:10
38

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.

Luke Stanley
  • 1,274
  • 1
  • 16
  • 32
Ryan
  • 15,016
  • 6
  • 48
  • 50
  • 7
    Andrew Bennetts recently wrote a couple of post about why he thinks doctest are abused. http://andrew.puzzling.org/diary/2008/October/23/narrative-tests http://andrew.puzzling.org/diary/2008/October/24/more-doctest-problems – ddaa Oct 24 '08 at 12:18
  • 4
    I think doctest actually is more aligned with the philosophy of BDD, when you treat it as it was intended: you start writing about the software, and then intersperse that with examples that also form tests. It's been described as "document driven development" as well -- the point is to focus on outward describable functionality, not internal units of work. I think tradition xUnit *is* horrible at doing that. – ianb Apr 20 '09 at 18:38
29

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.

matlehmann
  • 392
  • 3
  • 8
Douglas Camata
  • 598
  • 4
  • 10
  • Found this useful document while looking for details of your answer: http://arxiv.org/pdf/1007.1722 – amit kumar Sep 10 '11 at 10:37
  • I like should-dsl. I've been considering a DSL for python BDD - there are a few, this one seems quite expressive. – Danny Staple Mar 26 '12 at 12:30
  • I am not able to find any information about about a BDD framework called Pyramid. The link referenced in the paper linked by @phaedrus leads to a dubious looking site that doesn't have anything to do with testing, and googling points to [Pyramid](https://pypi.python.org/pypi/pyramid/), the web framework. Can anyone provide an up-to-date link? – Björn Pollex Aug 22 '13 at 12:28
  • 1
    I prefer the [sure](http://falcao.it/sure/) assertion DSL. – fatuhoku Sep 02 '13 at 09:33
  • @BjörnPollex, the name Pyramid couldn't be used by these tools creators because of the Pyramid Web Framework. Now they're only separated tools. – Douglas Camata Dec 18 '13 at 17:39
11

Great post and answers. Just wanted to update to include Freshen in this list as I read pycukes is discontinued. A good post about using BDD and Django with Freshen is here.

amitc
  • 365
  • 1
  • 4
  • 8
Steve
  • 715
  • 10
  • 18
9

You can use "sure" for expressive assertions (just like in RSpec)

Gabriel Falcão
  • 1,075
  • 1
  • 13
  • 9
  • Parabens! You totally blew my mind with the code in magic.py. I had no idea that "extension methods" (open classes) were possible in Python. – Michael Whatcott Nov 20 '12 at 06:06
8

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.

6

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)
Michael Whatcott
  • 5,603
  • 6
  • 36
  • 50
6

I like Pyccuracy a lot. I'm implementing it on a mid sized project these days.

Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
Refael Ackermann
  • 1,498
  • 1
  • 18
  • 24
4

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.

ddaa
  • 52,890
  • 7
  • 50
  • 59
  • TDD really is the revolutionizing practice in a totally different scale than BDD. Still I've come to appreciate way of writing test-drivenly in BDD style. – JtR Oct 24 '08 at 10:17
  • 1
    BDD started at the unit level, this is true. It grew fairly quickly to encompass system-level behaviour, where the contexts, events and outcomes benefit from a bit more reusability - hence the proliferation of tools to support both that and natural-language scenarios captured from conversations with non-technical stakeholders. Since this question was asked we've taken BDD up to project vision level, using Feature Injection, with the same emphasis on discovery through conversation and domain language. Still nothing new under the sun. – Lunivore Mar 07 '11 at 11:13
  • I like this talk about bdd http://www.youtube.com/watch?v=pherUEzdJow . I shows a good way to write specifications and have it as tests. – aisbaa Apr 04 '12 at 14:04