Your SlideShare is downloading. ×
  • Like

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

The Why and How of Automated Testing with Python and Django

  • 7,800 views
Published

A holistic view of the agile software engineering process with a focus on automated testing and continuous integration and python and django examples.

A holistic view of the agile software engineering process with a focus on automated testing and continuous integration and python and django examples.

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • The video is online here:

    <br /><object type="application/x-shockwave-flash" data="http://blip.tv/play/AYKI80oC" width="350" height="288"><param name="movie" value="http://blip.tv/play/AYKI80oC"></param><embed src="http://blip.tv/play/AYKI80oC" width="350" height="288" type="application/x-shockwave-flash"></embed></object>
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
7,800
On SlideShare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
79
Comments
1
Likes
5

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. The Why and How of Automated Testing with Python and Django Jim Purbrick BrightonPy 26th October 2010
  • 2. Who Am I? Technical Director at Linden Lab Setup Linden Lab Brighton Software Engineer Musician Freelancer Entrepreneur
  • 3. That’s No Moon... 35,000 CPU Cores Weekly Releases No Downtime 1.5 MLOC 100 Engineers 10 Years Image: jurvetson C++, Python, Perl, PHP, C#...
  • 4. This Is Different... Image: xkcd.com
  • 5. ...Or Is It? } django } python proxy { } wsgi cache { } httpd (no)sql { } linux } browser network { Image: carlsjacques
  • 6. Run Fast, Localise Errors } system tests } integration tests } unit tests Image: gothick_matt
  • 7. Process TDD CI Deploy
  • 8. Unit Tests from django.http import HttpResponseNotAllowed def allow(verb): def decorator(target): def wrapper(*args, **kwargs): if args[0].method != verb: return HttpResponseNotAllowed([verb]) return target(*args, **kwargs) return wrapper return decorator
  • 9. import unittest import http @http.allow('GET') def _test_function(request): return class TestRequest: def __init__(self, method): self.method = method class testAllowDecorator(unittest.TestCase): def testGetReturnsNone(self): self.assertIsNone(_test_function(TestRequest('GET'))) def testPostReturns405AllowsGet(self): respose = _test_function(TestRequest('POST')) self.failUnlessEqual(response.status, 405) self.assertEquals(response['Allow'], 'GET') if __name__ == '__main__': unittest.main()
  • 10. Integration Tests class LoggedInTestCase(LoggedInTestCase): urls = 'test_urls' fixtures = ['test_data.json'] def testActivityGetMethod200(self): client = self.createLoggedInClient() response = client.get('/activity/1/') self.failUnlessEqual(response.status_code, 200) def testActivityInvalidId404(self): client = self.createLoggedInClient() response = client.get('/activity/100000/') self.failUnlessEqual(response.status_code, 404) def testActivityPostMethodGetAllowed(self): client = self.createLoggedInClient() response = client.post('/activity/1/') self.failUnlessEqual(response.status_code, 405) self.assertEquals(response['Allow'], 'GET')
  • 11. System Tests from selenium import selenium import unittest class qunitTest(unittest.TestCase): def setUp(self): self.verificationErrors = [] self.selenium = selenium("localhost", 4444, "*chrome", "http://example.com/undo/") self.selenium.start() def testQunitTest(self): sel = self.selenium sel.open("/qunit/drawarr/undo/") sel.wait_for_page_to_load("10000") self.failUnless(sel.is_text_present("0 failed")) def tearDown(self): self.selenium.stop() self.assertEqual([], self.verificationErrors)
  • 12. Deployment def production(): env.hosts = ['production.com'] def ci(): env.hosts = ['localhost'] def setup(): require('hosts', provided_by=[production,ci]) sudo('aptitude install -y python-setuptools') sudo('easy_install pip') sudo('pip install virtualenv') sudo('aptitude install -y apache2') sudo('aptitude install -y libapache2-mod-wsgi')
  • 13. def deploy(): require('hosts', provided_by=[production,ci]) import time env.release = time.strftime('%Y%m%d%H%M%S') upload_tar_from_hg() install_requirements() install_site() create_settings_local() symlink_current_release() migrate() restart_webserver() def install_requirements(): require('release', provided_by=[deploy, setup]) run('cd %s; pip install -E . -r ./releases/%s/requirements.txt' % (env.path, env.release)) def migrate(): require('project_name') run('python manage.py syncdb --noinput' % (env.path, env.project_name)) run('manage.py migrate' % (env.path, env.project_name))
  • 14. CI #!/bin/bash export WORKON_HOME=~/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh if ! workon env then mkvirtualenv env fi set -x set -e fab install_requirements_local echo "from settings_development import *" > settings_local.py fab test
  • 15. #!/bin/bash # Start tracing and set exit on failure. set -x set -e # Deploy to ci environment # (pipe password in to sudo during deploy) echo "ci" | fab ci deploy # Add selenium python driver to python path. export PYTHONPATH=/usr/lib/selenium-python-client-driver-1.0.1:$ {PYTHONPATH} # Run python selenium tests via selenium-rc. python selenium/test.py python selenium/text/test.py python selenium/undo/test.py
  • 16. Demo?
  • 17. What’s Left? 1. Multiple Browsers/Platforms 2. Image Capture 3. Performance Tests 4. CD 5. Monitoring
  • 18. Thank You! http://jimpurbrick.com http://18dex.com