testing your django app - rtnpro.fedorapeople.org · unit testing is a method by which individual...
TRANSCRIPT
![Page 2: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/2.jpg)
Why test?● Check if your app behaves as it is supposed
to● Refactoring code does not break existing
app's behaviour
![Page 3: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/3.jpg)
Django test frameworks
● Doctests● Unit tests
![Page 4: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/4.jpg)
Doctests
def my_func(a_list, idx): """ >>> a = ['larry', 'curly', 'moe'] >>> my_func(a, 0) 'larry' >>> my_func(a, 1) 'curly' """ return a_list[idx]
Tests that are embedded in your functions' docstrings and are written in a way that emulates a session of the Python interactive interpreter
![Page 5: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/5.jpg)
Unit testsUnit testing is a method by which individual units of source code are tested to determine if they are fit for use
import unittest
class MyFuncTestCase(unittest.TestCase): def testBasic(self): a = ['larry', 'curly', 'moe'] self.assertEqual(my_func(a, 0), 'larry') self.assertEqual(my_func(a, 1), 'curly')
![Page 6: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/6.jpg)
Benefits of Unit tests
● Facilitates change● Simplifies Integration● Documentation● Design
![Page 7: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/7.jpg)
Writing unit tests● Django's unit tests use a Python standard library module: unittest.
This module defines tests in class-based approach.
from django.utils import unittest
● Django test runner looks for unit tests in two places:● models.py● tests.py, in the same directory as models.py
The test runner looks for any subclass of unittest.TestCase in this module.
![Page 8: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/8.jpg)
Writing unit testsfrom django.test import TestCasefrom django.test.client import Clientfrom posts.models import Post
class TestPosts(TestCase): def setUp(self): self.post = Post.objects.create(title='foo', body='foo')
def test_views(self): self.assertEqual(self.post.title, 'foo')
self.assertEqual(self.post.body, 'foo')
![Page 9: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/9.jpg)
Running tests● $ ./manage.py test● $ ./manage.py test posts● $ ./manage.py test posts.TestPosts● $ ./manage.py test posts.TestPosts.test_views
![Page 10: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/10.jpg)
The Test Client● A python class that acts as a dummy web browser● Allows testing your views● Interact with your Django app programatically
![Page 11: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/11.jpg)
Let's run tests
![Page 12: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/12.jpg)
The Test Client
● A way to build tests of the full stack● Acts more or less like a browser● Stateful● Default instance on django.test.TestCase● Can be instantiated outside test framework
![Page 13: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/13.jpg)
The test client isn't
● A live browser test framework● Selenium● Twill● Windmill
![Page 14: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/14.jpg)
Why is it different?
● TestClient can't do some things● No JavaScript● DOM validation or control
● Can do some things that browsers can't
![Page 15: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/15.jpg)
Returns a response
● response.status_code● response.content● response.cookies● response['Content-Disposition']
![Page 16: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/16.jpg)
... and a little more
● response.template● The template(s) used in rendering
● response.context● The context objects used in rendering● response.context['foo']
![Page 17: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/17.jpg)
...and it maintains state
● self.cookies● self.session
![Page 18: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/18.jpg)
Login/Logout
from django.test.client import Clientc = Client()c.login(username='foo',
Password='password')
c.logout()
![Page 19: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/19.jpg)
Get a page
from django.test.client import Client
c = Client()
c.get('/foo/')
c.get('/foo/?page=bar')
c.get('/foo/', {'page': 'bar'})
![Page 20: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/20.jpg)
Post a page
from django.test.client import Client
c = Client()
c.post('/foo/')
c.post('/foo/?bar=3')
c.post('/foo/', data={'bar': 3})
c.get('/foo/?whiz=4', data={'bar':3})
![Page 21: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/21.jpg)
Rest of HTTP
c.put('/foo/')
c.options('/foo/')
c.head('/foo/')
c.delete('/foo/')
![Page 22: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/22.jpg)
A common problem
r = self.client.get('/foo')self.assertEquals(r.status_code, 200)
FAIL Assertion Error: 301 != 200
![Page 23: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/23.jpg)
The fixr = self.client.get('/foo', follow=True)
self.assertEquals(r.status_code, 200)
response.redirect_chain -- links visited before a non-redirect was found
![Page 24: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/24.jpg)
You can do more
● Extra Headersc = TestClient(HTTP_HOST='foo.com')
c.get('/foo/', HTTP_HOST='foo.com')
● Filesf = open('text.txt')
c.post('/foo/',
content_type=MULTIPART_CONTENT),data = {'file': f})
f.close()
![Page 25: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/25.jpg)
Assertions
● assertContains()● assertNotContains()● assertFormError()● assertTemplateUser()● assertTemplateNotUsed()● assertRedirects()
![Page 26: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/26.jpg)
Do's and Don'ts
● Don't rely on assertContains● Assertions on template content are weak
● Test at the source● Is the context right?● Are the forms correct?
● Django's templates make this possible!
![Page 27: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/27.jpg)
When to use TestClient
● When you need to test the full stack● Interaction of view and middleware
● Great for testing idempotency
![Page 28: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/28.jpg)
Let's run some tests again
![Page 29: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/29.jpg)
Coverage
● Why do I need coverage tests?An small example:● You write some new code :-)● Does it really work as expected? :-(● Now, you write tests for it :-)● Does your test really test your code? :-(
This is where coverage comes to the rescue :D
![Page 30: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/30.jpg)
What is coverage● A tool for measuring code coverage of Python programs.
● It monitors your program, noting which parts of the code have
been executed,
● It then analyzes the source to identify code that could have been executed but was not.
● Coverage measurement is typically used to gauge the effectiveness of tests. It can show which parts of your code are being exercised by tests, and which are not.
![Page 31: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/31.jpg)
Who is behind Coverage
Ned Batchelder
![Page 32: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/32.jpg)
Install Coverage
● easy_install coverage● pip install coverage
![Page 33: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/33.jpg)
How to use Coverage?● Use coverage to run your program and gather data
● $ coverage -x my_program.py arg1 arg2● Use coverage to report on the results
$ coverage -rm
Name Stmts Miss Cover Missing
-------------------------------------------------------
my_program 20 4 80% 33-35, 39
my_other_module 56 6 89% 17-23
-------------------------------------------------------
TOTAL 76 10 87%
![Page 34: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/34.jpg)
How to use Coverage● Generate better reports for presentation, say html:
● coverage html -d <dir>● Erase existing coverage data
● coverage -e
![Page 35: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/35.jpg)
Let's use Coverage
![Page 36: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/36.jpg)
Questions?
![Page 37: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/37.jpg)
Useful links● https://docs.djangoproject.com/en/dev/topics/testing/
● http://nedbatchelder.com/code/coverage/
![Page 39: Testing your Django App - rtnpro.fedorapeople.org · Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest](https://reader036.vdocuments.us/reader036/viewer/2022071210/6021d79ea399f857e0592aff/html5/thumbnails/39.jpg)
Thank You :)