36

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

Embed Size (px)

DESCRIPTION

 

Citation preview

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

more than aside salad:behaviour driven testing andtest driven design in Djangowith Lettuce

Danielle Madeley blogs.gnome.org/danni dannipenguin

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

Just what is testdrivendevelopment?

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

Tests first;code second

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

http://www.nilkanth.com/2007/06/08/three­monkeys­of­test­driven­development/

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

But how do wewrite tests whenwe don't knowwhat the codelooks like?

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

Don't test code;test behaviours

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

As a visitor to the siteI want to create an account using my GoogleloginSo that I can log in without needing anotherpassword

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

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

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

Lettucehttp://lettuce.it

http://www.flickr.com/photos/65567316@N00/2742564457

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

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

Page 13: More than a side salad: behaviour driven testing and test driven design in Django with Lettuce
Page 14: 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

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

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)

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

Steps arestateful

http://www.flickr.com/photos/chrisconnell/3201514924/

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

from django.test.client import Clientfrom lettuce import before, step, worldfrom nose.tools import assert_equals

@before.each_scenariodef 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)

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

./manage.pyharvest

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

# settings.py

INSTALLED_APPS = ( ... 'lettuce.django', 'myapp',)

LETTUCE_APPS = ( 'myapp',)

LETTUCE_USE_TEST_DATABASE = True

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

Selenium

http://www.flickr.com/photos/21663307@N02/3599012763

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

from lettuce import before, world# import external lettuce stepsimport lettuce_webdriver.webdriver

@before.alldef set_browser(): """ Create a browser instance for use in tests. """

world.browser = webdriver.PhantomJS(...) world.browser.set_window_size(1200, 800)

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

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('/'))

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

Scenario: Add consumer with blank Contact name When I log in to admin with username "admin" and password "secret" 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

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

Fixtures

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

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 |

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

Built in steps usingDjango's modelintrospectionGiven I have users in the database: ...

Then there should be 1 consumer in the database

And consumer should be present in the database: ...

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

Which are extendable@creates_models(User)def create_user(step): data = hashes_data(step) for hash_ in data: is_superuser = hash_.pop('is_superuser', False

if is_superuser: user = User.objects.create_superuser(**hash_) else: user = User.objects.create_user(**hash_)

user.save()

reset_sequence(User)

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

Best practice

Page 29: More than a side salad: behaviour driven testing and test driven design in Django with Lettuce
Page 30: 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 checkoutAnd I pay for the items

Then everything worked

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

write reusablesteps

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

write reusable scenariosScenario Outline: I edit a user to add a team Given users have permissions: | user | organisation | permission | | [email protected] | OrgCorp | organisation_user | And I log in with email "<email>" through the profile server

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 database Then organisation member metadata should be present in the database: | organisation__name | user__email | team | | OrgCorp | [email protected] | Team 1 |

Examples: | email | | [email protected] | | [email protected] | | [email protected] |

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

tie each feature fileto a single story

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

include scenariosand anti-scenarios

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

Other uses

http://www.flickr.com/photos/omcoc/3050378171/

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

fin ;-Pquestions?

colophon This presentation was done in reveal.js using Junction, Cantarelland 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/