The document discusses strategies for testing code in Django. It recommends writing unit tests to verify functionality, regression tests to prevent reintroduced bugs, and acceptance tests to validate features. Common testing tools for Django include unittest, coverage.py, django.test, nose, and Selenium. Code that should be tested includes business logic, database interactions, complex code, and Django-specific elements. The document advises testing a little at a time and capturing bugs in automated tests to maintain a fast, reliable test suite.
2. Why test your code?
You need to verify that your code works.
Does your code do whats 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 doesnt blow up.
Load Tests
Goal: Measure response times and performance of software and system.
4. Tools
unittest package library
Part of pythons 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.
8. Things to live by
1. Something is better than nothing.
2. Coverage isnt everything.
3. Be willing to invest in test fixtures.
4. If you arent convinced on the value of testing, your
team wont 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.
9. Thank you. Questions?
www.roycehaynes.com
www.github.com/roycehaynes
@roycehaynes on Twitter