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 into our Virtualenv
- Using the FT to Check That Our Container Works
- Debugging a 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. Infrastructure As Code: Automated Deployments With Ansible
- Getting a Domain Name
- Manually Provisioning a Server to Host Our Site
- Configuring Domains for Staging and Live
- Ansible
- SSHing Into the Server and Viewing Container Logs
- Getting our image onto the server
- Using an env File to Store Our Environment Variables
- Mounting the database on the server and running migrations
- It workssss
- Further Reading
- 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
Head First Python, 3rd Edition
What will you learn from this book? Want to learn the Python language without slogging your …
book
Clean Code in Python - Second Edition
Tackle inefficiencies and errors the Pythonic way Key Features Enhance your coding skills using the new …
book
Python Object-Oriented Programming - Fourth Edition
A comprehensive guide to exploring modern Python through data structures, design patterns, and effective object-oriented techniques …