testing in django
DESCRIPTION
My presentation on why you should test in Django and how I like to do it.TRANSCRIPT
![Page 1: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/1.jpg)
Testing in Django
Why and how you should test in Django
![Page 2: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/2.jpg)
Code without tests is broken as designed
Jacob Kaplan-Moss
![Page 3: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/3.jpg)
The Pinax Example
http://code.pinax.com/tasks Problem: Anyone with account could edit ticket status.
![Page 4: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/4.jpg)
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.
![Page 5: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/5.jpg)
The Pinax Example
Easy fix: • Django already supports groups and permissions
• add a new permission method to the state_transition
tuple
![Page 6: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/6.jpg)
The Pinax Example
Works!• Looks fine in my browser!
• Looks fine in James Tauber's browser!
![Page 7: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/7.jpg)
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
![Page 8: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/8.jpg)
ERROR - Test Fail!
![Page 9: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/9.jpg)
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:
![Page 10: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/10.jpg)
Code without tests is broken as designed
Jacob Kaplan-Moss
![Page 11: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/11.jpg)
What about...
Selenium...Windchill...Nose...Twill...Your test system of choice...
All good stuff but outside the scope of this presentation.
![Page 12: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/12.jpg)
Doctest or Unittest?
![Page 13: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/13.jpg)
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
![Page 14: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/14.jpg)
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
![Page 15: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/15.jpg)
My pattern for Django unit tests
1.mkdir 'tests' folder in tasks2.touch test_authentication.py in tasks/tests 3.touch __init__.py folder in tasks/tests4.edit __init__.py to include:
from test_authentication.py import *
![Page 16: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/16.jpg)
My pattern for Django unit tests
The test setup:
![Page 17: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/17.jpg)
My pattern for Django unit tests
The test:
![Page 18: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/18.jpg)
My pattern for Django unit tests
Run the test!
python manage.py test tasks
![Page 19: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/19.jpg)
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.
![Page 20: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/20.jpg)
Things I like about tests
• Avoidance of embarrassment • Looks good
• The rush
![Page 21: Testing In Django](https://reader034.vdocuments.us/reader034/viewer/2022051314/554dad81b4c905ff7a8b4fa2/html5/thumbnails/21.jpg)
Questions? Comments?