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


Published on

Published in: Technology, Business
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

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

  1. 1. more than a side salad: behaviour driven testing and test driven design in Django with Lettuce Danielle Madeley   dannipenguin
  2. 2. Just what is test driven development?
  3. 3. Tests first; code second
  4. 4.­monkeys­of­test­driven­development/
  5. 5. But how do we write tests when we don't know what the code looks like?
  6. 6. Don't test code; test behaviours
  7. 7. 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
  8. 8. GivenI have a valid Google account WhenI visit the siteAnd I click create account ThenI am redirected to Google
  9. 9. Lettuce
  10. 10. 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
  11. 11. Steps
  12. 12. 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)
  13. 13. Steps are stateful
  14. 14. from django.test.client import Client from lettuce import before, step, world from 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)
  15. 15. ./ harvest
  16. 16. # INSTALLED_APPS = ( ... 'lettuce.django', 'myapp', ) LETTUCE_APPS = ( 'myapp', ) LETTUCE_USE_TEST_DATABASE = True
  17. 17. Selenium
  18. 18. 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)
  19. 19. 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('/'))
  20. 20. 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 "" And I press "Save" Then I should see "This field is required" And there should be 0 consumers in the database
  21. 21. Fixtures
  22. 22. 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 |
  23. 23. 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: ...
  24. 24. 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_) reset_sequence(User)
  25. 25. Best practice
  26. 26. Given I have items in my cart When I go to checkout And I pay for the items Then everything worked
  27. 27. write reusable steps
  28. 28. write reusable scenarios Scenario Outline: I edit a user to add a team Given users have permissions: | user | organisation | permission | | | 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 | | Team 1 | Examples: | email | | | | | | |
  29. 29. tie each feature file to a single story
  30. 30. include scenarios and anti-scenarios
  31. 31. Other uses
  32. 32. 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!   dannipenguin