Testing In Django
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Testing In Django

on

  • 3,942 views

My presentation on why you should test in Django and how I like to do it.

My presentation on why you should test in Django and how I like to do it.

Statistics

Views

Total Views
3,942
Views on SlideShare
3,924
Embed Views
18

Actions

Likes
4
Downloads
66
Comments
0

3 Embeds 18

http://www.slideshare.net 10
http://www.linkedin.com 6
https://www.linkedin.com 2

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Testing In Django Presentation Transcript

  • 1. Testing in Django Why and how you should test in Django
  • 2. Code without tests  is broken as designed   Jacob Kaplan-Moss
  • 3. The Pinax Example
    • http://code.pinax.com/tasks
    •  
    • Problem: Anyone with account could edit ticket status.
    •  
  • 4. The Pinax Example
    • Example of why this needed to be changed:
    •  
      • User adds ticket to a release already under feature freeze.
    •  
      • User moves it to resolved.
    •  
      • Core developers have to remember every ticket.
    •  
  • 5. The Pinax Example
    • Easy fix:
    •  
      • Django already supports groups and permissions 
    •  
      • add a new permission method to the state_transition tuple
    •  
  • 6. The Pinax Example
    • Works!
      • Looks fine in my browser!
    •  
      • Looks fine in James Tauber's browser!
  • 7. Flashback
    • Pycon 2009
      • Daniel Mizyrycki, pythonista and hang glider enthusiast
    •  
      • Nixes the ability for anonymous users to add/modify tasks
    •  
      • Writes two tests to confirm his change works
  • 8. ERROR - Test Fail!  
  • 9. The Pinax Example
    • Daniel's tests toss out an error!
      • Anonymous users break
    •  
      • is_task_manager method doesn't allow for task viewing by anonymous users
    •  
      • Fix is easy:
    •  
    •  
    •  
  • 10. Code without tests  is broken as designed   Jacob Kaplan-Moss
  • 11. What about...
    • Selenium...
    • Windchill...
    • Nose...
    • Twill...
    • Your test system of choice...
    • All good stuff but outside the scope of this presentation.
  • 12. Doctest or Unittest?
  • 13. My pros and cons of Doctest
    • Pros
    •  
      • Easy to learn
    •  
      • Documents your code
    •  
      • Is pretty with RST
    • Cons
    •  
      • Kinda hard to maintain
    •  
      • Weirdness on assertions
  • 14. My pattern for Django unit tests
      • Via Django Admin, add some sample data
    •  
      • In tasks application, mkdir fixtures
    •  
      • Via command line:
    •  
    •  
      ./manage.py dumpdata tasks > apps/tasks/fixtures/test_tasks.json  
  • 15. My pattern for Django unit tests
      • mkdir 'tests' folder in tasks
      • touch test_authentication.py in tasks/tests
      • touch __init__.py folder in tasks/tests
      • edit __init__.py to include:
    •  
    •      from test_authentication.py import *
  • 16. My pattern for Django unit tests
    • The test setup:
  • 17. My pattern for Django unit tests
    • The test:
  • 18. My pattern for Django unit tests
    • Run the test!
    •      python manage.py test tasks
  • 19. A bigger test
    • See how each method I write tests a different thing.
    •  
    • Seems like a good pattern right now.
    • But you always find your old tests look silly.
    • You get better at writing tests the more you do it.
  • 20. Things I like about tests
      • Avoidance of embarrassment
    •  
      • Looks good
    •  
      • The rush
  • 21. Questions? Comments?