Test-Driven Development with Python
Test-Driven Development with Python
Summary:
Introduction
Harry Percival's book "Test-Driven Development with Python" is not just another Python textbook. It is a full-fledged practical guide that immerses the reader in the world of Test-Driven Development (TDD) using the example of building a real web application. Published in 2017 (second edition), the book remains relevant today, especially for those who want to master professional Python development practices using Django and Selenium.
The author, Harry Percival, is a lead engineer at PythonAnywhere, known for his experience in testing and web development. He doesn't just explain theory; he guides the reader step by step from an empty directory to a fully working, tested application.
What is this book about?
The book is dedicated to the Test-Driven Development methodology as applied to web development in Python. The main project is creating a simple To-Do list application based on Django. However, the real value of the book is not in the project itself, but in the process: you will learn to write tests before the code is written and see how this changes the architecture and quality of the final product.
The book covers:
- Basics of TDD: Red-Green-Refactor.
- Functional tests with Selenium.
- Unit tests for Django (models, views, forms).
- Testing JavaScript with QUnit and Sinon.js.
- Continuous Integration (CI) with Travis CI.
- Deployment on a server (PythonAnywhere, Heroku).
- Working with databases, migrations, and test fixtures.
- Mock objects and testing external services.
Who is this book for?
The book is intended for developers already familiar with the basics of Python and having a basic understanding of web frameworks (preferably Django, but not required). If you:
- want to write more reliable and maintainable code;
- are tired of "debugging via print" and want to automate checks;
- plan to work in a team where TDD is the standard;
- want to understand how to test not only the backend but also the frontend (JavaScript);
— this book is for you. Beginners in Python will find it challenging: the author assumes you know how to install packages, work with a virtual environment, and understand the basics of OOP.
What will the reader learn?
After reading the book, you will be able to:
- Write tests before code and see how it speeds up development.
- Use Selenium to simulate user actions in the browser.
- Test Django applications at all levels: from models to templates.
- Refactor code with confidence that tests will catch regressions.
- Set up CI/CD for automatic test runs.
- Work with Git in the context of TDD (frequent commits, atomic changes).
- Write tests for JavaScript and integrate them into the overall process.
Strengths of the book
1. Practical focus. You don't just read theory — you write code along with the author. Each chapter contains specific steps, terminal commands, and explanations.
2. Gradual increase in complexity. It starts with a simple "Hello, world" in tests and ends with deployment to a production server with HTTPS and static files.
3. Realistic project. The To-Do list application is not a toy example, but a full-fledged project that can be extended.
4. Coverage of modern tools. Selenium, Django, Git, Travis CI, PythonAnywhere — all of these are actually used in the industry.
5. Detailed explanations. The author doesn't just give commands, but explains why it is done this way,