SlideShare a Scribd company logo
more than a
side salad:
behaviour driven testing and
test driven design in Django
with Lettuce
Danielle Madeley
 blogs.gnome.org/danni  dannipenguin
Just what is test
driven
development?
More than a side salad: behaviour driven testing and test driven design in Django with Lettuce
Tests first;
code second
http://www.nilkanth.com/2007/06/08/three­monkeys­of­test­driven­development/
But how do we
write tests when
we don't know
what the code
looks like?
Don't test code;
test behaviours
More than a side salad: behaviour driven testing and test driven design in Django with Lettuce
As a visitor to the site
I want to create an account using my Google
login
So that I can log in without needing another
password
GivenI have a valid Google account
WhenI visit the siteAnd I click create account
ThenI am redirected to Google
http://www.flickr.com/photos/27369469@N08/2660160225
Lettuce
http://lettuce.it
http://www.flickr.com/photos/65567316@N00/2742564457
Feature: Authenticate to API
As a searcher/API user
I want to authenticate to the API
So that I can make queries
Scenario: I am not authenticated to the API
When I get the resource "/api/v3/hello/"
Then I get the response code 401
More than a side salad: behaviour driven testing and test driven design in Django with Lettuce
Steps
http://www.flickr.com/photos/60364452@N00/504844510
from lettuce import step, world
@step(r'I get the resource "([^"]*)"')
def get_resource(step, url):
"""
Make a GET request to the given URL
"""
world.response = world.client.get(url)
Steps are
stateful
http://www.flickr.com/photos/chrisconnell/3201514924/
from django.test.client import Client
from lettuce import before, step, world
from nose.tools import assert_equals
@before.each_scenario
def set_default_client(scenario):
world.client = Client()
@step(r'I get the resource "([^"]*)"')
def get_resource(step, url):
world.response = world.client.get(url)
@step(r'I get the response code (d+)')
def check_response_code(step, code):
code = int(code)
assert_equals(world.response.status_code, code)
./manage.py
harvest
# settings.py
INSTALLED_APPS = (
...
'lettuce.django',
'myapp',
)
LETTUCE_APPS = (
'myapp',
)
LETTUCE_USE_TEST_DATABASE = True
Selenium
http://www.flickr.com/photos/21663307@N02/3599012763
from lettuce import before, world
# import external lettuce steps
import lettuce_webdriver.webdriver
@before.all
def set_browser():
"""
Create a browser instance for use in tests.
"""
world.browser = webdriver.PhantomJS(...)
world.browser.set_window_size(1200, 800)
def site_url(url):
base_url = 'http://%s' % socket.gethostname()
if server.port is not 80:
base_url += ':%d' % server.port
return urlparse.urljoin(base_url, url)
@step("I visit the site")
def open_site(step):
step.given('I visit "%s"' % site_url('/'))
Scenario: Add consumer with blank Contact name
When I log in to admin with username "admin" and
And I click "Consumers"
And I click "Add consumer"
And I fill in "Organisation" with "OOOO"
And I fill in "Contact name" with ""
And I fill in "Contact number" with "0399999999"
And I fill in "Website" with "www.test.com"
And I press "Save"
Then I should see "This field is required"
And there should be 0 consumers in the database
Fixtures
Feature: Create new API key
As an administrator
I want to create a new API key
So that I can enable others to query API
Background:
Given I have users in the database:
| username | password | is_superuser |
| admin | secret | true |
Built in steps using
Django's model
introspection
Given I have users in the database:
...
Then there should be 1 consumer in the database
And consumer should be present in the database:
...
Which are extendable
@creates_models(User)
def create_user(step):
data = hashes_data(step)
for hash_ in data:
is_superuser = hash_.pop('is_superuser', Fals
if is_superuser:
user = User.objects.create_superuser(**ha
else:
user = User.objects.create_user(**hash_)
user.save()
reset_sequence(User)
Best practice
More than a side salad: behaviour driven testing and test driven design in Django with Lettuce
Given I have items in my cart
When I go to checkout
And I pay for the items
Then everything worked
write reusable
steps
write reusable scenarios
Scenario Outline: I edit a user to add a team
Given users have permissions:
| user | organisation | permission |
| peon@org.org | OrgCorp | organisation_user |
And I log in with email "<email>" through the profile serve
When I visit site page "organisation/1/admin/user/4/edit"
And I select option "Team 1" from selector "Team"
And I press "Save"
Then there should be 1 organisation member metadata in the
Then organisation member metadata should be present in the
| organisation__name | user__email | team |
| OrgCorp | peon@org.org | Team 1 |
Examples:
| email |
| god@god.org |
| superuser@org.org |
| poweruser@org.org |
tie each feature file
to a single story
include scenarios
and anti-scenarios
Other uses
http://www.flickr.com/photos/omcoc/3050378171/
fin ;-P
questions?
colophon This presentation was done in reveal.js using Junction, Cantarell
and Source Code Pro with photography from the Creative Commons.
Infoxchange is hiring. Come and chat to me!
 blogs.gnome.org/danni  dannipenguin
http://www.flickr.com/photos/mau3ry/3763640652/

More Related Content

PDF
Django の認証処理実装パターン / Django Authentication Patterns
PDF
Djangoアプリのデプロイに関するプラクティス / Deploy django application
PPT
Gadgets Intro (Plus Mapplets)
ZIP
Renegades Guide to Hacking Rails Internals
ODP
Wellrailed - Be9's Acl9
PPTX
Introduction to Jquery
PPTX
Devise and Rails
PDF
Pundit
Django の認証処理実装パターン / Django Authentication Patterns
Djangoアプリのデプロイに関するプラクティス / Deploy django application
Gadgets Intro (Plus Mapplets)
Renegades Guide to Hacking Rails Internals
Wellrailed - Be9's Acl9
Introduction to Jquery
Devise and Rails
Pundit

What's hot (19)

PDF
Introduction to Magento 2 module development - PHP Antwerp Meetup 2017
PDF
Plugging into plugins
PDF
devise tutorial - 2011 rubyconf taiwan
PDF
PDF
Dicas de palestra
PDF
Manipulating Magento - Meet Magento Belgium 2017
ODP
Widgets: Making Your Site Great and Letting Others Help - WordCamp Victoria
KEY
Enter the app era with ruby on rails (rubyday)
PDF
Ionic으로 모바일앱 만들기 #3
ZIP
Unit Tests Aren't Enough
PPT
SES Toronto 2008; Joe Dolson
PDF
The Wide World Of Google Developer Technologies (STLIC 02-10)
PPTX
Setting up Page Object Model in Automation Framework
PDF
A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...
PDF
jQtouch, Building Awesome Webapps
PPTX
Intro to Rails Give Camp Atlanta
DOCX
Using HTML code to add page number and its output are there..
PPTX
Testing C# and ASP.net using Ruby
PPTX
Catalog display
Introduction to Magento 2 module development - PHP Antwerp Meetup 2017
Plugging into plugins
devise tutorial - 2011 rubyconf taiwan
Dicas de palestra
Manipulating Magento - Meet Magento Belgium 2017
Widgets: Making Your Site Great and Letting Others Help - WordCamp Victoria
Enter the app era with ruby on rails (rubyday)
Ionic으로 모바일앱 만들기 #3
Unit Tests Aren't Enough
SES Toronto 2008; Joe Dolson
The Wide World Of Google Developer Technologies (STLIC 02-10)
Setting up Page Object Model in Automation Framework
A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...
jQtouch, Building Awesome Webapps
Intro to Rails Give Camp Atlanta
Using HTML code to add page number and its output are there..
Testing C# and ASP.net using Ruby
Catalog display
Ad

Similar to More than a side salad: behaviour driven testing and test driven design in Django with Lettuce (20)

PDF
Behavior Driven Development with Cucumber
PDF
An Introduction to Django Web Framework
PDF
Controller Testing: You're Doing It Wrong
PDF
Introduction to Web APIs and the Google+ API - BarCamp Phnom Penh 2011
PDF
Leveraging Rails to Build Facebook Apps
PDF
Designing and Implementing a Multiuser Apps Platform
PPTX
Getting Started with Test Automation: Introduction to Cucumber with Lapis Lazuli
PDF
Google App Engine tutorial
PPT
OpenSocial Intro
PDF
Easy Step-by-Step Guide to Develop REST APIs with Django REST Framework
PDF
Web driver selenium simplified
PDF
Finding things on the web with BOSS
PPTX
Django Girls Tutorial
PDF
Developing WordPress Plugins : For Begineers
PDF
Gits class #22: [ONLINE] Analyze Your User's Activities Using BigQuery and Da...
PPT
CiviCRM API v3
PPTX
Customer Automation Masterclass - Workshop 1: Data Enrichment using Clearbit
PDF
Hacking For Innovation
PPTX
Improving LibGuides Editors Access to Use Data Using the Google Analytics API
PDF
Google analytics reporting using api
Behavior Driven Development with Cucumber
An Introduction to Django Web Framework
Controller Testing: You're Doing It Wrong
Introduction to Web APIs and the Google+ API - BarCamp Phnom Penh 2011
Leveraging Rails to Build Facebook Apps
Designing and Implementing a Multiuser Apps Platform
Getting Started with Test Automation: Introduction to Cucumber with Lapis Lazuli
Google App Engine tutorial
OpenSocial Intro
Easy Step-by-Step Guide to Develop REST APIs with Django REST Framework
Web driver selenium simplified
Finding things on the web with BOSS
Django Girls Tutorial
Developing WordPress Plugins : For Begineers
Gits class #22: [ONLINE] Analyze Your User's Activities Using BigQuery and Da...
CiviCRM API v3
Customer Automation Masterclass - Workshop 1: Data Enrichment using Clearbit
Hacking For Innovation
Improving LibGuides Editors Access to Use Data Using the Google Analytics API
Google analytics reporting using api
Ad

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Cloud computing and distributed systems.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Electronic commerce courselecture one. Pdf
PDF
Encapsulation theory and applications.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Empathic Computing: Creating Shared Understanding
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPT
Teaching material agriculture food technology
PPTX
Big Data Technologies - Introduction.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Cloud computing and distributed systems.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Digital-Transformation-Roadmap-for-Companies.pptx
Spectral efficient network and resource selection model in 5G networks
Electronic commerce courselecture one. Pdf
Encapsulation theory and applications.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Empathic Computing: Creating Shared Understanding
Building Integrated photovoltaic BIPV_UPV.pdf
Programs and apps: productivity, graphics, security and other tools
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Teaching material agriculture food technology
Big Data Technologies - Introduction.pptx
The AUB Centre for AI in Media Proposal.docx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx

More than a side salad: behaviour driven testing and test driven design in Django with Lettuce