two scope of django 1.6 chapter 20 and 21

21
20. Testing Stinks and Is a Waste of Money! & 21. Documentation: Be Obsessed 2015/02/24 Eric 1

Upload: gu-yuan-lin

Post on 18-Jul-2015

124 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Two Scope of Django 1.6  Chapter 20 and 21

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

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

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

20. Testing Stinks and Is a Waste of Money!

4

Page 5: Two Scope of Django 1.6  Chapter 20 and 21

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

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

How to Write Unit Tests?

• Each Test Method Tests One Thing

7

Page 8: Two Scope of Django 1.6  Chapter 20 and 21

How to Write Unit Tests?

8

Page 9: Two Scope of Django 1.6  Chapter 20 and 21

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

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

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

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

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

21. Documentation: Be Obsessed

14

Page 15: Two Scope of Django 1.6  Chapter 20 and 21

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

RST Example

Sphinx

16

Page 17: Two Scope of Django 1.6  Chapter 20 and 21

What Docs Should Django Projects Contain?

17

Page 18: Two Scope of Django 1.6  Chapter 20 and 21

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

Web API Documentation• Django REST Framework + Django REST Swagger

19

Page 20: Two Scope of Django 1.6  Chapter 20 and 21

Sample Code

• https://github.com/guyuanlin/two_scope_of_django

20

Page 21: Two Scope of Django 1.6  Chapter 20 and 21

Thank You!

21