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
- Praise for Test-Driven Development with Python
- Preface
- Prerequisites and Assumptions
- Acknowledgments
-
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?
-
5. Saving User Input
- Wiring Up Our Form to Send a POST Request
- 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
- Rendering Items in the Template
- Creating Our Production Database with migrate
-
6. Getting to the Minimum Viable Site
- Ensuring Test Isolation in Functional Tests
- Small Design When Necessary
- Implementing the New Design Using TDD
- Iterating Towards the New Design
- Testing Views, Templates, and URLs Together with the Django Test Client
- Another URL and View for Adding List Items
- Adjusting Our Models
- Each List Should Have Its Own URL
- One More View to Handle Adding Items to an Existing List
- A Final Refactor Using URL includes
-
II. Web Development Sine Qua Nons
-
7. Prettification: Layout and Styling, and What to Test About It
- What to Functionally Test About Layout and Style
- Prettification: Using a CSS Framework
- Django Template Inheritance
- Integrating Bootstrap
- Static Files in Django
- Using Bootstrap Components to Improve the Look of the Site
- Using Our Own CSS
- What We Glossed Over: collectstatic and Other Static Directories
- A Few Things That Didn’t Make It
- 8. Testing Deployment Using a Staging Site
- 9. Automating Deployment with Fabric
- 10. Input Validation and Test Organisation
- 11. A Simple Form
- 12. More Advanced Forms
- 13. Dipping Our Toes, Very Tentatively, into JavaScript
- 14. Deploying Our New Code
-
7. Prettification: Layout and Styling, and What to Test About It
-
III. More Advanced Topics
-
15. User Authentication, Integrating Third-Party Plugins, and Mocking with JavaScript
- Mozilla Persona (BrowserID)
- Exploratory Coding, aka “Spiking”
- De-spiking
- JavaScript Unit Tests Involving External Components: Our First Mocks!
- 16. Server-Side Authentication and Mocking in Python
- 17. Test Fixtures, Logging, and Server-Side Debugging
-
18. Finishing “My Lists”: Outside-In TDD
- The Alternative: “Inside Out”
- Why Prefer “Outside-In”?
- The FT for “My Lists”
- The Outside Layer: Presentation and Templates
- Moving Down One Layer to View Functions (the Controller)
- Another Pass, Outside-In
- The Next “Requirement” from the Views Layer: New Lists Should Record Owner
- Moving Down to the Model Layer
-
19. Test Isolation, and “Listening to Your Tests”
- Revisiting Our Decision Point: The Views Layer Depends on Unwritten Models Code
- A First Attempt at Using Mocks for Isolation
- Listen to Your Tests: Ugly Tests Signal a Need to Refactor
- Rewriting Our Tests for the View to Be Fully Isolated
- Moving Down to the Forms Layer
- Finally, Moving Down to the Models Layer
- The Moment of Truth (and the Risks of Mocking)
- Thinking of Interactions Between Layers as “Contracts”
- One More Test
- Tidy Up: What to Keep from Our Integrated Test Suite
- Conclusions: When to Write Isolated Versus Integrated Tests
- 20. Continuous Integration (CI)
- 21. The Token Social Bit, the Page Pattern, and an Exercise for the Reader
- 22. Fast Tests, Slow Tests, and Hot Lava
- Obey the Testing Goat!
- A. PythonAnywhere
- B. Django Class-Based Views
- C. Provisioning with Ansible
- D. Testing Database Migrations
-
E. What to Do Next
- Notifications—Both on the Site and by Email
- Switch to Postgres
- Run Your Tests Against Different Browsers
- 404 and 500 Tests
- The Django Admin Site
- Investigate a BDD Tool
- Write Some Security Tests
- Test for Graceful Degradation
- Caching and Performance Testing
- JavaScript MVC Frameworks
- Async and Websockets
- Switch to Using py.test
- Client-Side Encryption
- Your Suggestion Here
- F. Cheat Sheet
- G. Bibliography
-
15. User Authentication, Integrating Third-Party Plugins, and Mocking with JavaScript
- Index
- Colophon
- Copyright
Product information
- Title: Test-Driven Development with Python
- Author(s):
- Release date: June 2014
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781449364823
You might also like
book
Using Asyncio in Python
If you’re among the Python developers put off by asyncio’s complexity, it’s time to take another …
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
Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 3rd Edition
Through a recent series of breakthroughs, deep learning has boosted the entire field of machine learning. …
book
Designing Large Language Model Applications
Transformer-based language models are powerful tools for solving a variety of language tasks and represent a …