Upload
gu-yuan-lin
View
124
Download
1
Embed Size (px)
Citation preview
20. Testing Stinks and Is a Waste of Money! &
21. Documentation: Be Obsessed2015/02/24
Eric
1
Self-introduction
• Eric
• 勇敢吧!智慧科技 (BraveBot Intelligent)
• iOS + Android APP Customization
• Backend Engineer
• 1 year experience with Django
2
Outline• 20. Testing Stinks and Is a Waste of Money!
• Why Tests?
• How to Structure Tests?
• How to Write Unit Tests?
• The Game of Test Coverage
• 21. Documentation: Be Obsessed
• Sphinx & reStructuredText
• What Docs Should Django Project Contains?
• Other Documentation Methods
3
20. Testing Stinks and Is a Waste of Money!
4
Why Tests• Testing Saves Money, Jobs, and Lives
• Handling medical information
• Works with other people’s money now or will at some point in the future
• Providing life-critical resources to people in need
• Testing Saves Time
• Refactoring
• Upgrading Django version
5
How to Structure Tests?
• Delete the default and useless tests.py module
• Create a tests directory and create __init__.py, test_models.py, …
• Must prefix modules with test_
• So we can config discover runner to discover just test files.
6
How to Write Unit Tests?
• Each Test Method Tests One Thing
7
How to Write Unit Tests?
8
How to Write Unit Tests?• Don’t Write Tests That Have to Be Tested
• DRY Doesn’t Apply to Writing Tests
• Don’t Rely on Fixtures
• Fixtures are hard to maintain as a project’s data change over time
• Modify JSON-formatted files to match your last migration is hard
• It’s easier to write code that relies on the ORM
• Tools to generate test data
• factory_boy : http://factoryboy.readthedocs.org/en/latest/
• model_mommy : http://model-mommy.readthedocs.org/en/latest/basic_usage.html
• mock : http://mock.readthedocs.org/en/latest/
9
How to Write Unit Tests?
• Things that should be tested
• Views, Models, Forms, Validators, Signals, Filters, Template Tags, …
• Things that shouldn’t be tested
• Django core, third-party packages
10
The Game of Test Coverage
• Mandate that no commit can lower test coverage
11
The Game of Test Coverage
• Step 1: set up a test runner
• create settings/test.py
• Step 2 : run tests and generate coverage report
• install coverage module
• coverage run manage.py test —settings=project_name.settings.test
12
The Game of Test Coverage• Step 3 : generate the report !
• coverage html —include=`pwd`/*—omit=‘env/lib/*.py,*/tests/*.py,*/migrations/*.py’
• automatically create the htmlcov/ directory containing the index.html
13
21. Documentation: Be Obsessed
14
reStructuredText & Sphinx• reStructuredText(RST)
• Most common markup language used for documenting Python projects
• Sphinx
• Generating docs from .rst files
• Output formats
• HTML, LaTeX, manual pages, and plain text
• pip install Sphinx
15
RST Example
Sphinx
16
What Docs Should Django Projects Contain?
17
Other Documentation Methods
• Wikis
• Online document stores
• Google Drive, Dropbox, box.net, …
• They are better than no documents
18
Web API Documentation• Django REST Framework + Django REST Swagger
19
Sample Code
• https://github.com/guyuanlin/two_scope_of_django
20
Thank You!
21