Your SlideShare is downloading. ×
0
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Testing In Django
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Testing In Django

2,673

Published on

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.

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,673
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
68
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

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 <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. 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. 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. 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&apos;s browser! </li></ul></ul>
  • 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. ERROR - Test Fail!  
  • 9. The Pinax Example <ul><li>Daniel&apos;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&apos;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. Code without tests  is broken as designed   Jacob Kaplan-Moss
  • 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. Doctest or Unittest?
  • 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. 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 &gt; apps/tasks/fixtures/test_tasks.json  
  • 15. My pattern for Django unit tests <ul><ul><li>mkdir &apos;tests&apos; 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. My pattern for Django unit tests <ul><li>The test setup: </li></ul>
  • 17. My pattern for Django unit tests <ul><li>The test: </li></ul>
  • 18. My pattern for Django unit tests <ul><li>Run the test! </li></ul><ul><li>     python manage.py test tasks </li></ul>
  • 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. 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. Questions? Comments?

×