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 how to write and run tests before building each part of your app, and then develop the minimum amount of code required to pass those tests. The result? Clean code that works.
In the process, you'll learn the basics of Django, Selenium, Git, jQuery, and Mock, along with current web development techniques. If you're ready to take your Python skills to the next level, this book clearly demonstrates how TDD encourages simple designs and inspires confidence.
- Dive into the TDD workflow, including the unit test/code cycle and refactoring;
- Use unit tests for classes and functions, and functional tests for user interactions within the browser;
- Learn when and how to use mock objects, and the pros and cons of isolated vs. integrated tests;
- Test and automate your deployments with a staging server;
- Apply tests to the third-party plugins you integrate into your site;
- Use a Continuous Integration environment to run your tests automatically.
This open book is licensed under a Creative Commons License (CC BY-NC-SA). Free download in PDF format is not available. You can read Test-Driven Development with Python book online for free.
Table of Contents
The Basics of TDD and Django
Getting Django Set Up Using a Functional Test
Extending Our Functional Test Using the unittest Module
Testing a Simple Home Page with Unit Tests
What Are We Doing with All These Tests? (And, Refactoring)
Saving User Input: Testing the Database
Improving Functional Tests: Ensuring Isolation and Removing Voodoo Sleeps
Web Development Sine Qua Nons
Prettification: Layout and Styling, and What to Test About It
Testing Deployment Using a Staging Site
Getting to a Production-Ready Deployment
Automating Deployment with Fabric
Splitting our tests into multiple files, and a generic wait helper
Validation at the Database Layer
A Simple Form
More Advanced Forms
Deploying Our New Code
More Advanced Topics
User Authentication, Spiking and De-Spiking
Using Mocks to Test External Dependencies or Reduce Duplication
Test Fixtures and a Decorator for Explicit Waits
Finishing "My Lists": Outside-In TDD
Test Isolation, and "Listening to Your Tests"
Continuous Integration (CI)
The Token Social Bit, the Page Pattern, and an Exercise for the Reader
Fast Tests, Slow Tests, and Hot Lava