Top Banner
Testing in Django Why and how you should test in Django
21

Testing In Django

May 09, 2015

Download

Self Improvement

My presentation on why you should test in Django and how I like to do it.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Testing In Django

Testing in Django

Why and how you should test in Django

Page 2: Testing In Django

Code without tests is broken as designed

 Jacob Kaplan-Moss

Page 3: Testing In Django

The Pinax Example

http://code.pinax.com/tasks Problem: Anyone with account could edit ticket status. 

Page 4: Testing In Django

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

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

The Pinax Example

Works!• Looks fine in my browser!

 • Looks fine in James Tauber's browser!

Page 7: Testing In Django

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

ERROR - Test Fail!

 

Page 9: Testing In Django

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

Code without tests is broken as designed

 Jacob Kaplan-Moss

Page 11: Testing In Django

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

Doctest or Unittest?

Page 13: Testing In Django

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

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

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

My pattern for Django unit tests

The test setup:

Page 17: Testing In Django

My pattern for Django unit tests

The test:

Page 18: Testing In Django

My pattern for Django unit tests

Run the test!

    python manage.py test tasks

Page 19: Testing In Django

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

Things I like about tests

• Avoidance of embarrassment • Looks good

 • The rush

Page 21: Testing In Django

Questions? Comments?