Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django Testing Strategy

Django Testing Strategy

A talk I did about testing code you write, and a test strategy with Django web framework.

Royce Haynes

March 01, 2013
Tweet

Other Decks in Programming

Transcript

  1. B Y R O Y C E H A Y

    N E S T W I T T E R : @ R O Y C E H A Y N E S 2 9 J A N 2 0 1 3 A Testing Strategy in Django
  2. Why test your code? —  You need to verify that

    your code works. ¡  Does your code do what’s expected, and nothing else? —  Provide usage examples for the next guy. ¡  Can other developers see examples of how your code is used? —  Use your API before you make it. ¡  Map out how you would use code before writing it. —  Minimize mistakes, maximize productivity ¡  Writing tests saves time, money, and lives.
  3. Types of Testing —  Unit Tests ¡  Goal: Interrogates a

    unit of your source code. ¡  Typically repeatable, consistent, and fast. ¡  Try to mock complex dependencies and interactions to the outside world. —  Regression Tests ¡  Goal: Verify that a bug never happens again. ¡  Fixes known software bugs. ¡  Uncovers new software bugs. —  Acceptance Tests ¡  Goal: Verify a feature is correctly implemented. —  Smoke Tests ¡  Goal: Ensure software and system doesn’t blow up. —  Load Tests ¡  Goal: Measure response times and performance of software and system.
  4. Tools —  unittest package library ¡  Part of python’s core

    lib. ¡  Most used package for testing web applications. —  coverage.py ¡  Tool used to measure coverage of Python programs, not just Django. —  django.test ¡  Django.test is pretty much the unittest package library. —  nose ¡  Extends unittest lib and can make automated testing easier. —  django-discover-runner ¡  A python acceptance test tool for web applications. —  Jenkins CI ¡  Use to test and deploy code. —  Selenium ¡  Automate web application acceptance test.
  5. What to test? —  Business logic ¡  Credit card processing

    ¡  Creating, updating, or removing database records —  Shady Code ¡  Shady code is untested code that appears confusing or looks wrong. —  Specific to Django ¡  Custom views and forms ¡  Overriding methods core to Django ¡  Code that relies on untested third-party packages (optional).
  6. When should you test? —  Code a little, test a

    little (or vice versa) —  If you use version control system, successfully test code before pushing to remote repo.
  7. Things to live by 1.  Something is better than nothing.

    2.  Coverage isn’t everything. 3.  Be willing to invest in test fixtures. 4.  If you aren’t convinced on the value of testing, your team won’t be either. 5.  Harvest metrics. 6.  Capture bugs in an automated test. 7.  Pause to refactor when test suite takes too long to run 8.  Be ready to throw away an entire day of changes.