Oleg Pidsadnyi - Behaviour driven design with PyTest

published May 10, 2013

Oleg Pidsadnyi talks about behaviour driven design with PyTest, at PyGrunn.

See the PyGrunn website for more info about this one-day Python conference in Groningen, The Netherlands.

Code of pytest-bdd: https://github.com/olegpidsadnyi/pytest-bdd

I will talk about behaviour driven development in Python. What is it? You define a scenario with strict language, like given this and that, expect this. It is readable for both programmers and business logic persons.

There are several ways to do this. You can use Lettuce or Freshen, plus Splinter. But imperative coding style does not work well here, I think. Given I have two books. How do you do that? context.books = [...]? context.book1, context.book2? pytest-bdd has the concept of expecting and returning values, with @pytest.fixture:

def author():
    return Author()

def book(author):
    return Book(author=author)

@given('I have two books')
def article(author):
    return [book(author=author), book(author=author)]

@given('I have an article')
def article(author):
    return create_test_article(author=author)

This is much more explicit.

It can do browser tests. You can use a normal browser, like Firefox, or you can set it up headless with phantomjs.

pytest-bdd is inspired by the Robot Framework, but we had some different requirements.