A Testing Strategy in Django BY ROYCE HAYNES TWITTER: @ROYCEHAYNES
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.
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.
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.
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).
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.
Things to live by1. 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 run8. Be ready to throw away an entire day of changes.
Thank you. Questions? www.roycehaynes.com www.github.com/roycehaynes @roycehaynes on Twitter