Testing Django Applications


Published on

Quick introduction to testing django applications. Given at the January meeting of DJUGL in London.

Published in: Technology
1 Comment
  • Nice Presentation !
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Testing Django Applications

  1. 1. ..............................E. ========================================================= ERROR: test_admin_related_links_presence (apps.pages.integration_tests.frontend.FrontendTest) --------------------------------------------------------- Ran 32 tests in 132.851s FAILED (errors=1) Destroying test database... Practical Testing for Django Developers DJUGL 19th January 2008 gareth rushgrove | morethanseven.net
  2. 2. Not Simon Willison gareth rushgrove | morethanseven.net
  3. 3. Not Simon Willison Gareth Rushgrove gareth rushgrove | morethanseven.net
  4. 4. Who Writes Tests? Own Up. gareth rushgrove | morethanseven.net
  5. 5. Python v2.6.1 documentation » The Python Standard Library » Development Tools » previous | next | modules | index — Unit testing framework Table Of Contents unittest — Unit testing unittest framework New in version 2.1. Basic example Organizing test code The Python unit testing framework, sometimes referred to as “PyUnit,” is a Re-using old test code Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit Classes and functions is, in turn, a Java version of Kent!s Smalltalk testing framework. Each is TestCase Objects TestSuite Objects the de facto standard unit testing framework for its respective language. TestResult Objects TestLoader Objects supports test automation, sharing of setup and shutdown code unittest Previous topic for tests, aggregation of tests into collections, and independence of the tests from the reporting framework. The unittest module provides — Test interactive doctest classes that make it easy to support these qualities for a set of tests. Python examples Next topic To achieve this, supports some important concepts: unittest 2to3 - Automated Python 2 to 3 code translation test fixture A test fixture represents the preparation needed to perform one or This Page more tests, and any associate cleanup actions. This may involve, for Show Source example, creating temporary or proxy databases, directories, or Quick search starting a server process. Testing Python with PyUnit test case Go A test case is the smallest unit of testing. It checks for a specific response to a particular set of inputs. unittest provides a base class, Enter search terms or a module, class or function name. TestCase , which may be used to create new test cases. test suite gareth rushgrove | morethanseven.net A test suite is a collection of test cases, test suites, or both. It is used
  6. 6. ...F................E. Pass, Fail, Error gareth rushgrove | morethanseven.net
  7. 7. Again, remember that you can use both systems side-by-side (even in the same app). In the end, most projects will eventually end up using both. Each shines in different circumstances. Running tests Once you've written tests, run them using your project's manage.py utility: $ ./manage.py test By default, this will run every test in every application in INSTALLED_APPS. If you only want to run tests for a particular application, add the application name to the command line. For example, if your INSTALLED_APPS contains 'myproject.polls' and 'myproject.animals', you can run the myproject.animals unit tests alone with this command: $ ./manage.py test animals Note that we used animals, not myproject.animals. New in Django 1.0: You can now choose which test to run. If you use unit tests, as opposed to doctests, you can be even more specific in choosing which tests to execute. To run a single test case in an application (for example, the AnimalTestCase described in the quot;Writing unit testsquot; section), add the name of the test case to the label on the command line: $ ./manage.py test animals.AnimalTestCase Django Test Runner And it gets even more granular than that! To run a single test method inside a test case, add the name of the test method to the label: $ ./manage.py test animals.AnimalTestCase.testFluffyAnimals The test database gareth rushgrove | morethanseven.net
  8. 8. What to Test - Low Level Code gareth rushgrove | morethanseven.net
  9. 9. - Functions - Input/output - Object methods - Object creation - Action at a distance via signals Unit Tests and System Tests gareth rushgrove | morethanseven.net
  10. 10. What to Test - High Level Code gareth rushgrove | morethanseven.net
  11. 11. - HTTP Status codes - Fragments of HTML from templatetags - Broken links - Presence of markup on pages - Rendered HTML - Check admin registration - Functionality Functional Tests gareth rushgrove | morethanseven.net
  12. 12. Past the Basics gareth rushgrove | morethanseven.net
  13. 13. Custom Assertions gareth rushgrove | morethanseven.net
  14. 14. garethr account | profile | guides | log out repositories: all | search 0 Source Commits Graphs Wiki (1) Watchers (1) Network (1) Fork Queue Admin master all branches all tags garethr / django-test-extensions Description: A set of custom assertions and examples for use testing django applications edit Homepage: Click to edit edit Public Clone URL: git://github.com/garethr/django-test-extensions.git Your Clone URL: git@github.com:garethr/django-test-extensions.git commit c327bac72d990af890c231e33d0e146a79b7c507 refactor into setup tools module and include custom test runners tree 92148ec270467b41cb7d5ef5f194a14b44192d8d garethr (author) parent 85237c52d0afd22eb3c1af1e7d639f21fa5bfde9 November 24, 2008 django-test-extensions / name age message history .gitignore November 24, 2008 refactor into setup tools module and include cu... [garethr] README November 22, 2008 seperated out django assertions and added README [garethr] setup.py November 24, 2008 refactor into setup tools module and include cu... [garethr] Common Base Class src/ November 24, 2008 refactor into setup tools module and include cu... [garethr] PyUnit provides a basic set of assertions which can get you started with unit testing python, but it's always useful to have more. Django also has a few specific requirements and common patterns when it comes to testing. This set of classes aims to provide a useful starting point for both these situations. gareth rushgrove | morethanseven.net
  15. 15. Custom Test Runner gareth rushgrove | morethanseven.net
  16. 16. Name Stmts Exec Cover ---------------------------------------- __init__ 2 0 0% loader 10 0 0% main 85 56 65% models 5 0 0% settings 2 0 0% ---------------------------------------- TOTAL 104 56 53% Coverage Reporting gareth rushgrove | morethanseven.net
  17. 17. Tools Integration gareth rushgrove | morethanseven.net
  18. 18. Unit System Functional Integration Separate Test Suites gareth rushgrove | morethanseven.net
  19. 19. Ellington’s test suite, which was taking around 1.5-2 hours to run on Postgres, has been reduced to 10 minutes. ericholscher Speed Improvements gareth rushgrove | morethanseven.net
  20. 20. http://flickr.com/photos/psd/102332391/ http://flickr.com/photos/ijames/112866961/ http://flickr.com/photos/venancio2007/3059620452/ http://flickr.com/photos/bigdogwoody2000/2540302958/ http://flickr.com/photos/pigatto/333486434/ Flickr Credits
  21. 21. Go Forth And Write Tests gareth rushgrove | morethanseven.net