two scope of django 1.6 chapter 20 and 21
TRANSCRIPT
![Page 1: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/1.jpg)
20. Testing Stinks and Is a Waste of Money! &
21. Documentation: Be Obsessed2015/02/24
Eric
1
![Page 2: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/2.jpg)
Self-introduction
• Eric
• 勇敢吧!智慧科技 (BraveBot Intelligent)
• iOS + Android APP Customization
• Backend Engineer
• 1 year experience with Django
2
![Page 3: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/3.jpg)
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
![Page 4: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/4.jpg)
20. Testing Stinks and Is a Waste of Money!
4
![Page 5: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/5.jpg)
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
![Page 6: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/6.jpg)
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
![Page 7: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/7.jpg)
How to Write Unit Tests?
• Each Test Method Tests One Thing
7
![Page 8: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/8.jpg)
How to Write Unit Tests?
8
![Page 9: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/9.jpg)
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
![Page 10: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/10.jpg)
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
![Page 11: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/11.jpg)
The Game of Test Coverage
• Mandate that no commit can lower test coverage
11
![Page 12: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/12.jpg)
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
![Page 13: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/13.jpg)
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
![Page 14: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/14.jpg)
21. Documentation: Be Obsessed
14
![Page 15: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/15.jpg)
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
![Page 16: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/16.jpg)
RST Example
Sphinx
16
![Page 17: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/17.jpg)
What Docs Should Django Projects Contain?
17
![Page 18: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/18.jpg)
Other Documentation Methods
• Wikis
• Online document stores
• Google Drive, Dropbox, box.net, …
• They are better than no documents
18
![Page 19: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/19.jpg)
Web API Documentation• Django REST Framework + Django REST Swagger
19
![Page 20: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/20.jpg)
Sample Code
• https://github.com/guyuanlin/two_scope_of_django
20
![Page 21: Two Scope of Django 1.6 Chapter 20 and 21](https://reader035.vdocuments.mx/reader035/viewer/2022081515/55a9f0cd1a28ab9e418b47c5/html5/thumbnails/21.jpg)
Thank You!
21