Cover image for Test-Driven Development with Python

Book description

By taking you through the development of a real web application from beginning to end, this hands-on guide demonstrates the practical advantages of test-driven development (TDD) with Python. You’ll learn the basics of Django, Selenium, Git, jQuery, and Mock, along with current web development techniques.

Table of Contents

  1. Preface
    1. Why I wrote a book about Test-Driven Development
    2. Aims of this book
    3. Outline
    4. Some pre-requisites
      1. Python 3 & web programming
      2. How HTML works
      3. Required software installations:
      4. Required Python modules:
    5. Conventions Used in This Book
    6. Contacting O’Reilly
  2. 1. Getting Django set up using a Functional Test
    1. Obey the Testing Goat: Do nothing until you have a test
    2. Getting Django up and running
    3. Starting a Git repository
  3. 2. Extending our Functional Test using the unittest module
    1. Using the Functional Test to scope out a minimum viable app
    2. The Python standard library’s
    3. Implicitly wait
    4. Commit
  4. 3. Testing a simple home page with unit tests
    1. Our first Django app, and our first unit test
    2. Unit tests, and how they differ from Functional tests
    3. Unit testing in Django
    4. Django’s MVC, URLs and view functions
    5. At last! We actually write some application code!
    6. urls.py
    7. Unit testing a view
      1. The unit test / code cycle
  5. 4. What are we doing with all these tests?
    1. A moment’s reflection - what are we up to?
    2. Using Selenium to test user interactions
    3. The “Don’t test constants” rule, and templates to the rescue
    4. On refactoring
    5. A little more of our front page
    6. Recap: the TDD process
  6. 5. Saving user input
    1. Wiring up our form to send a POST request
    2. Processing a POST request on the server
    3. 3 strikes and refactor
    4. The Django ORM & our first model
    5. Saving the POST to the database
    6. Redirect after a POST
    7. Rendering items in the template
    8. Creating our production database with syncdb
  7. 6. Getting to the minimum viable site
    1. Ensuring test isolation in functional tests
    2. Small Design When Necessary
      1. YAGNI!
      2. REST
    3. Implementing the new design using TDD
    4. Iterating towards the new design
    5. Testing views, templates and URLs together with the Django Test Client
    6. Adding another URL
    7. Adjusting our models
    8. The final stage: each list should have its own URL
    9. A final refactor using URL includes
  8. 7. Prettification: layout and styling, and what to test about it
    1. What to functionally test about layout and style
    2. Prettification: Using a CSS framework
    3. Django template inheritance
    4. Integrating Bootstrap
    5. Static files in Django
    6. Final bits of fiddling
    7. What we skipped over: collectstatic and other static directories
  9. 8. Testing deployment using a staging site
    1. TDD and the Danger Areas of deployment
    2. As always, start with a test
    3. Manually provisioning a server to host our site
      1. Choosing where to host our site
      2. Spinning up a server
      3. Installing Nginx
      4. Configuring domains for staging and live
    4. Deploying our code manually
      1. Adjusting the database location
      2. Creating a virtualenv
      3. Simple nginx configuration
      4. Creating the database with syncdb
      5. Switching to Gunicorn
      6. Getting Nginx to serve static files
      7. Switching to using Unix sockets
      8. Switching DEBUG to False and setting ALLOWED_HOSTS
      9. Using upstart to make sure gunicorn starts on boot
      10. Saving our changes: adding gunicorn to our requirements.txt
    5. Automating:
    6. Automating deployment with fabric
    7. Recap:
    8. Further reading:
    9. Todos
  10. 9. Outline to date & future chapters plan
    1. BOOK 1: Building a minimum viable app with TDD
    2. BOOK 2: Growing the site
      1. Chapter 9: User Authentication + the admin site
      2. Chapter 10: A more complex model, forms and validation
      3. Chapter 11: javascript
      4. Chapter 11: Ajax
      5. Chapter 12: sharing lists
      6. Chapter 13: oauth
      7. More/Other possible contents
    3. BOOK 3: Trendy stuff
      1. Chapter 14: CI
      2. Chapter 15 & 16: More Javascript
      3. Chapter 17: Async
      4. Chapter 18: NoSQL
      5. Chapter 19: Caching
    4. Appendices
      1. Other possible appendix(?) topics
      2. Existing appendix I: PythonAnywhere
  11. A. PythonAnywhere
    1. Running Firefox Selenium sessions with pyVirtualDisplay
    2. Setting up Django as a PythonAnywhere web app
    3. Cleaning up /tmp
    4. Screenshots
  12. Bibliography
  13. About the Author
  14. Copyright