Book description
The third edition of this trusted guide demonstrates the practical advantages of test-driven development (TDD) with Python and describes how to develop a real web application. 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, author Harry Percival teaches software and web developers the basics of Django, Selenium, Git, JavaScript, and Mock libraries, along with current web development techniques. This book—updated for Python 3.11 and Django 4—clearly demonstrates how TDD encourages simple designs and inspires confidence.
Fully updated, this third edition addresses:
- The TDD workflow, including the unit test/code cycle and refactoring
- Unit tests for classes and functions and functional tests for user interactions within the browser
- Mock objects and the pros and cons of isolated versus integrated tests
- Testing and automation of deployments with a staging server
- Tests applied to the third-party plug-ins you integrate into your site
- Automatic tests using a continuous integration environment
- Using TDD to build a REST API with a JavaScript frontend interface
Publisher resources
Table of contents
- Brief Table of Contents (Not Yet Final)
- Preface
- Prerequisites and Assumptions
- Companion Video
- I. The Basics of TDD and Django
- 1. Getting Django Set Up Using a Functional Test
- 2. Extending Our Functional Test Using the unittest Module
- 3. Testing a Simple Home Page with Unit Tests
- 4. What Are We Doing with All These Tests? (And, Refactoring)
-
5. Saving User Input: Testing the Database
- Wiring Up Our Form to Send a POST Request
- Debugging functional tests
- Processing a POST Request on the Server
- Passing Python Variables to Be Rendered in the Template
- Three Strikes and Refactor
- The Django ORM and 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 migrate
- Recap
- 6. Improving Functional Tests: Ensuring Isolation and Removing Voodoo Sleeps
-
7. Working Incrementally
- Small Design When Necessary
- Implementing the New Design Incrementally Using TDD
- Ensuring We Have a Regression Test
- Iterating Towards the New Design
- Taking a First, Self-Contained Step: One New URL
- Another Small Step: A Separate Template for Viewing Lists
- A Third Small Step: A New URL for Adding List Items
- Biting the Bullet: Adjusting Our Models
- Each List Should Have Its Own URL
- The Functional Tests Detect Another Regression
- One More View to Handle Adding Items to an Existing List
- A Final Refactor Using URL includes
- II. Web Development Sine Qua Nons
- 8. Prettification: Layout and Styling, and What to Test About It
-
9. Containerization aka Docker
- TDD and the Danger Areas of Deployment
- Docker, Containers and Virtualization
- Docker and the Danger Areas of Deployment
- An Overview of Our Deployment Procedure
- As Always, Start with a Test
- Installing Docker
- Building a Docker Image and Running a Docker Container
- Installing Django in a Virtualenv in our Container Image
- Using the FT to Check That Our Container Works
- Debugging Container Networking Problems
- Running code “inside” the container with docker exec
- Database migrations
- Mounting files inside the container.
- 10. Making Our App Production-Ready
- 11. Getting A Server Ready For Deployment
- 12. Infrastructure As Code: Automated Deployments With Ansible
- 13. Splitting Our Tests into Multiple Files, and a Generic Wait Helper
- 14. Validation at the Database Layer
- 15. A Simple Form
- 16. More Advanced Forms
-
17. A Gentle Excursion Into JavaScript
- Starting with an FT
- A Quick “Spike”
- Setting Up a Basic JavaScript Test Runner
- Testing with some DOM content
- Building a JavaScript Unit Test for Our Desired Functionality
- Fixtures, Execution Order, and Global State: Key Challenges of JS Testing
- Using an Initialize Function for More Control Over Execution Time
- Deliberately Breaking Our Code to Force Ourselves To Write More Tests
- Red, Green, Refactor: Removing Hardcoded Selectors
- Does it work?
- Testing Integration with CSS and Bootstrap
- Columbo Says: wait for Onload
- JavaScript Testing in the TDD Cycle
- 18. Deploying Our New Code
- About the Author
Product information
- Title: Test-Driven Development with Python, 3rd Edition
- Author(s):
- Release date: July 2025
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781098148713
You might also like
book
Test-Driven Development with Python, 2nd Edition
By taking you through the development of a real web application from beginning to end, the …
book
Fluent Python, 2nd Edition
Don't waste time bending Python to fit patterns you've learned in other languages. Python's simplicity lets …
book
Head First Python, 3rd Edition
What will you learn from this book? Want to learn the Python language without slogging your …
book
Think Python, 3rd Edition
Python is an excellent way to get started in programming, and this clear, concise guide walks …