Test-Driven Web Development with Python
Chapters
Log In / Sign Up
sorted by:
Relevance
Author(s)
Title
returning
5
10
20
50
100
values at a time.
Test-Driven Web Development with Python
Next
Test-Driven Web Development with Python
Table of Contents
Preface
Why I wrote a book about Test-Driven Development
Aims of this book
Outline
Some pre-requisites
Python 3 & programming
How HTML works
Required software installations:
Required Python modules:
Conventions Used in This Book
Contacting O’Reilly
1. Getting Django set up using a Functional Test
Obey the Testing Goat! Do nothing until you have a test
Getting Django up and running
Starting a Git repository
2. Extending our Functional Test using the unittest module
Using the Functional Test to scope out a minimum viable app
The Python standard library’s
unittest
module
Implicitly wait
Commit
3. Testing a simple home page with unit tests
Our first Django app, and our first unit test
Unit tests, and how they differ from Functional tests
Unit testing in Django
Django’s MVC, URLs and view functions
At last! We actually write some application code!
Reading tracebacks
urls.py
Unit testing a view
The unit test / code cycle
4. What are we doing with all these tests?
Programming is like pulling a bucket of water up from a well
On the merits of trivial tests for trivial functions
Using Selenium to test user interactions
The “Don’t test constants” rule, and templates to the rescue
On refactoring
A little more of our front page
Recap: the TDD process
5. Saving user input
Wiring up our form to send a POST request
Processing a POST request on the server
3 strikes and refactor
The Django ORM & our first model
Saving the POST to the database
Redirect after a POST
Better unit testing practice: each test should test one thing
Rendering items in the template
Creating our production database with syncdb
6. Getting to the minimum viable site
Ensuring test isolation in functional tests
Small Design When Necessary
YAGNI!
REST
Implementing the new design using TDD
Iterating towards the new design
Testing views, templates and URLs together with the Django Test Client
Adding another URL
Adjusting our models
The final stage: each list should have its own URL
A final refactor using URL includes
7. Prettification: layout and styling, and what to test about it
What to functionally test about layout and style
Prettification: Using a CSS framework
Django template inheritance
Integrating Bootstrap
Static files in Django
Final bits of fiddling
What we skipped over: collectstatic and other static directories
8. Testing deployment using a staging site
TDD and the Danger Areas of deployment
As always, start with a test
Getting a domain name
Manually provisioning a server to host our site
Choosing where to host our site
Spinning up a server
Installing Nginx
Configuring domains for staging and live
Deploying our code manually
Adjusting the database location
Creating a virtualenv
Simple nginx configuration
Creating the database with syncdb
Switching to Gunicorn
Getting Nginx to serve static files
Switching to using Unix sockets
Switching DEBUG to False and setting ALLOWED_HOSTS
Using upstart to make sure gunicorn starts on boot
Saving our changes: adding gunicorn to our requirements.txt
Automating:
Automating deployment with fabric
Recap:
Further reading:
Todos
9. Forms and input validation
Validation: preventing blank and duplicate list items
Using model-layer validation
Refactoring unit tests into several files
Unit testing model validation and the self.assertRaises context manager
Overriding the save method on a model to ensure validation
Handling model validation errors in the view:
Django pattern: processing POST request in the same view as renders the form
Refactor: Removing hard-coded URLs
The {% url %} template tag
Another FT for duplicate items
Dontification
Preventing duplicates at the model layer
A little digression on Queryset ordering and string representations
Handling validation at the views layer
Returning different error messages for different validation errors
Moving validation logic into a form
Switching to a Django ModelForm
Using the form in our home page
A more complex form to handle uniqueness validation
Using the existing lists item form in the list view
10. Outline to date & future chapters plan
BOOK 1: Building a minimum viable app with TDD
BOOK 2: Growing the site
Chapter 9: Forms and user input validation
Chapter 10: Simple javascript
Chapter 11: Authentication, 3rd party service integration & the admin site
Chapter 12: Ajax
Chapter 13: sharing lists
More/Other possible contents
BOOK 3: Trendy stuff
Chapter 14: CI
Chapter 15 & 16: More Javascript
Chapter 17: Async
Chapter 18: Caching
Appendices
Possible appendix topics
Existing appendix I: PythonAnywhere
A. PythonAnywhere
No Python 3
Running Firefox Selenium sessions with pyVirtualDisplay
Setting up Django as a PythonAnywhere web app
Cleaning up /tmp
Screenshots
B. Django Class-Based Views
Class-based generic views
The home page as a FormView
Using form_valid to customise a CreateView
A more complex view to handle both viewing and adding to a list
Best practices for unit testing CBGVs?
Bibliography