Testing in Django Why and how you should test in Django
Code without tests  is broken as designed   Jacob Kaplan-Moss
The Pinax Example <ul><li>http://code.pinax.com/tasks </li></ul><ul><li>  </li></ul><ul><li>Problem: Anyone with account c...
The Pinax Example <ul><li>Example of why this needed to be changed: </li></ul><ul><li>  </li></ul><ul><ul><li>User adds ti...
The Pinax Example <ul><li>Easy fix: </li></ul><ul><li>  </li></ul><ul><ul><li>Django already supports groups and permissio...
The Pinax Example <ul><li>Works! </li></ul><ul><ul><li>Looks fine in my browser! </li></ul></ul><ul><li>  </li></ul><ul><u...
Flashback <ul><li>Pycon 2009 </li></ul><ul><ul><li>Daniel Mizyrycki, pythonista and hang glider enthusiast </li></ul></ul>...
ERROR - Test Fail!  
The Pinax Example <ul><li>Daniel's tests toss out an error! </li></ul><ul><ul><li>Anonymous users break </li></ul></ul><ul...
Code without tests  is broken as designed   Jacob Kaplan-Moss
What about... <ul><li>Selenium... </li></ul><ul><li>Windchill... </li></ul><ul><li>Nose... </li></ul><ul><li>Twill... </li...
Doctest or Unittest?
My pros and cons of Doctest <ul><li>Pros </li></ul><ul><li>  </li></ul><ul><ul><li>Easy to learn </li></ul></ul><ul><li>  ...
My pattern for Django unit tests <ul><ul><li>Via Django Admin, add some sample data </li></ul></ul><ul><li>  </li></ul><ul...
My pattern for Django unit tests <ul><ul><li>mkdir  'tests'  folder in tasks </li></ul></ul><ul><ul><li>touch test_authent...
My pattern for Django unit tests <ul><li>The test setup: </li></ul>
My pattern for Django unit tests <ul><li>The test: </li></ul>
My pattern for Django unit tests <ul><li>Run the test! </li></ul><ul><li>     python manage.py test tasks </li></ul>
A bigger test <ul><li>See how each method I write tests a different thing. </li></ul><ul><li>  </li></ul><ul><li>Seems lik...
Things I like about tests <ul><ul><li>Avoidance of embarrassment </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Looks good...
Questions? Comments?
Upcoming SlideShare
Loading in...5
×

Testing In Django

2,709

Published on

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

Testing In Django

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

    Clipping is a handy way to collect important slides you want to go back to later.

×