• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
DjangoCon09: The Test Client
 

DjangoCon09: The Test Client

on

  • 4,503 views

The Django Test client is one of the more powerful testing tools in the Django test arsenal. However, in order to get the most out of the test client, it's important to understand what it is for - and ...

The Django Test client is one of the more powerful testing tools in the Django test arsenal. However, in order to get the most out of the test client, it's important to understand what it is for - and what it isn't for.

This howto will give an overview of the Django test client - when to use it, what it can be used for, and when to use something else entirely.

Statistics

Views

Total Views
4,503
Views on SlideShare
4,494
Embed Views
9

Actions

Likes
3
Downloads
60
Comments
0

1 Embed 9

http://www.slideshare.net 9

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    DjangoCon09: The Test Client DjangoCon09: The Test Client Presentation Transcript

    • The Test Client Dr Russell Keith-Magee DjangoCon 2009
    • What is it? • A way to build unit tests of the full stack • Acts “kinda-sorta” like a browser • Stateful • Default instance on django.test.TestCase • Can be instantiated outside test framework
    • The test client isn’t • A live browser test framework • Selenium • Twill • Windmill
    • Why is it different? • TestClient can’t do some things • No JavaScript • DOM validation or control • Can do some things that browsers can’t
    • GET a page c = TestClient() c.get(‘/foo/’) c.get(‘/foo/’, data={‘bar’:3}) c.get(‘/foo/?bar=3’)
    • Returns a response • response.status_code • response.content • response.cookies • response[‘Content-Disposition’]
    • ... and a little more • response.template • The template(s) used used in rendering • response.context • The context objects used in rendering • response.context[‘foo’]
    • ... and it maintains state • self.cookies • self.session
    • Login/Logout c.login(username='foo', password='password') c.logout()
    • POST a page c = TestClient() c.post(‘/foo/’) c.post(‘/foo/?bar=3’) c.post(‘/foo/’, data={‘bar’: 3}) c.get(‘/foo/?whiz=4’, data={‘bar’:3})
    • Rest of HTTP c.put(‘/foo/’) c.options(‘/foo/’) c.head(‘/foo/’) c.delete(‘/foo/’)
    • A common gotcha r = self.client.get(‘/foo’) self.assertEquals(r.status_code, 200) FAIL Assertion Error: 301 != 200
    • The fix r = self.client.get(‘/foo’, follow=True) self.assertEquals(r.status_code, 200) response.redirect_chain ➡ links visited before a non-redirect was found.
    • Extra Headers c = TestClient(HTTP_HOST=‘foo.com’) c.get(‘/foo/’, HTTP_HOST=‘foo.com’) response['X-DJANGO-TEST']
    • Files c = TestClient() f = open(‘mydata.txt’) c.post(‘/foo/’, content_type=MULTIPART_CONTENT data = {‘file’: f}) f.close()
    • Assertions • assertContains() • assertNotContains() • assertFormError() • assertTemplateUsed() • assertTemplateNotUsed() • assertRedirects()
    • Advice • 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!
    • When to use TestClient • When you need to test the full stack • interaction of view and middleware • Great for testing idempotency
    • Interesting hack • Use Client as a factory for Request objects • Ticket #9002, Snippet #963 • Then: • Construct a request object • Use to test a single view/middleware call
    • Fin