Test-Driven Development with Python, 2nd Edition

Book description

By taking you through the development of a real web application from beginning to end, the second edition of 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—updated for Python 3.6—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
  • Run tests automatically by using a Continuous Integration environment
  • Use TDD to build a REST API with a front-end Ajax interface

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Why I Wrote a Book About Test-Driven Development
    2. Aims of This Book
    3. Outline
    4. Conventions Used in This Book
    5. Submitting Errata
    6. Using Code Examples
    7. O’Reilly Safari
    8. Contacting O’Reilly
  2. Prerequisites and Assumptions
    1. Python 3 and Programming
    2. How HTML Works
    3. Django
    4. JavaScript
    5. Required Software Installations
      1. Git’s Default Editor, and Other Basic Git Config
      2. Installing Firefox and Geckodriver
    6. Setting Up Your Virtualenv
      1. Activating and Deactivating the Virtualenv
    7. Installing Django and Selenium
    8. Some Error Messages You’re Likely to See When You Inevitably Fail to Activate Your Virtualenv
  3. Companion Video
  4. Acknowledgments
    1. Additional Thanks for the Second Edition
  5. I. The Basics of TDD and Django
  6. 1. Getting Django Set Up Using a Functional Test
    1. Obey the Testing Goat! Do Nothing Until You Have a Test
    2. Getting Django Up and Running
    3. Starting a Git Repository
  7. 2. Extending Our Functional Test Using the unittest Module
    1. Using a Functional Test to Scope Out a Minimum Viable App
    2. The Python Standard Library’s unittest Module
    3. Commit
  8. 3. Testing a Simple Home Page with Unit Tests
    1. Our First Django App, and Our First Unit Test
    2. Unit Tests, and How They Differ from Functional Tests
    3. Unit Testing in Django
    4. Django’s MVC, URLs, and View Functions
    5. At Last! We Actually Write Some Application Code!
    6. urls.py
    7. Unit Testing a View
      1. The Unit-Test/Code Cycle
  9. 4. What Are We Doing with All These Tests? (And, Refactoring)
    1. Programming Is Like Pulling a Bucket of Water Up from a Well
    2. Using Selenium to Test User Interactions
    3. The “Don’t Test Constants” Rule, and Templates to the Rescue
      1. Refactoring to Use a Template
      2. The Django Test Client
    4. On Refactoring
    5. A Little More of Our Front Page
    6. Recap: The TDD Process
  10. 5. Saving User Input: Testing the Database
    1. Wiring Up Our Form to Send a POST Request
    2. Processing a POST Request on the Server
    3. Passing Python Variables to Be Rendered in the Template
      1. An Unexpected Failure
    4. Three Strikes and Refactor
    5. The Django ORM and Our First Model
      1. Our First Database Migration
      2. The Test Gets Surprisingly Far
      3. A New Field Means a New Migration
    6. Saving the POST to the Database
    7. Redirect After a POST
      1. Better Unit Testing Practice: Each Test Should Test One Thing
    8. Rendering Items in the Template
    9. Creating Our Production Database with migrate
    10. Recap
  11. 6. Improving Functional Tests: Ensuring Isolation and Removing Voodoo Sleeps
    1. Ensuring Test Isolation in Functional Tests
      1. Running Just the Unit Tests
    2. Aside: Upgrading Selenium and Geckodriver
    3. On Implicit and Explicit Waits, and Voodoo time.sleeps
  12. 7. Working Incrementally
    1. Small Design When Necessary
      1. Not Big Design Up Front
      2. YAGNI!
      3. REST (ish)
    2. Implementing the New Design Incrementally Using TDD
    3. Ensuring We Have a Regression Test
    4. Iterating Towards the New Design
    5. Taking a First, Self-Contained Step: One New URL
      1. A New URL
      2. A New View Function
    6. Green? Refactor
    7. Another Small Step: A Separate Template for Viewing Lists
    8. A Third Small Step: A URL for Adding List Items
      1. A Test Class for New List Creation
      2. A URL and View for New List Creation
      3. Removing Now-Redundant Code and Tests
      4. A Regression! Pointing Our Forms at the New URL
    9. Biting the Bullet: Adjusting Our Models
      1. A Foreign Key Relationship
      2. Adjusting the Rest of the World to Our New Models
    10. Each List Should Have Its Own URL
      1. Capturing Parameters from URLs
      2. Adjusting new_list to the New World
    11. The Functional Tests Detect Another Regression
    12. One More View to Handle Adding Items to an Existing List
      1. Beware of Greedy Regular Expressions!
      2. The Last New URL
      3. The Last New View
      4. Testing the Response Context Objects Directly
    13. A Final Refactor Using URL includes
  13. II. Web Development Sine Qua Nons
  14. 8. Prettification: Layout and Styling, and What to Test About It
    1. What to Functionally Test About Layout and Style
    2. Prettification: Using a CSS Framework
    3. Django Template Inheritance
    4. Integrating Bootstrap
      1. Rows and Columns
    5. Static Files in Django
      1. Switching to StaticLiveServerTestCase
    6. Using Bootstrap Components to Improve the Look of the Site
      1. Jumbotron!
      2. Large Inputs
      3. Table Styling
    7. Using Our Own CSS
    8. What We Glossed Over: collectstatic and Other Static Directories
    9. A Few Things That Didn’t Make It
  15. 9. Testing Deployment Using a Staging Site
    1. TDD and the Danger Areas of Deployment
    2. As Always, Start with a Test
    3. Getting a Domain Name
    4. Manually Provisioning a Server to Host Our Site
      1. Choosing Where to Host Our Site
      2. Spinning Up a Server
      3. User Accounts, SSH, and Privileges
      4. Installing Python 3.6
      5. Configuring Domains for Staging and Live
    5. Deploying Our Code Manually
      1. Creating a Virtualenv on the Server Using requirements.txt
      2. Using the FT to Check That Our Deployment Works
    6. Debugging a Deployment That Doesn’t Seem to Work at All
    7. Hacking ALLOWED_HOSTS in settings.py
    8. Creating the Database with migrate
    9. Success! Our Hack Deployment Works
  16. 10. Getting to a Production-Ready Deployment
    1. What We Need to Do
    2. Switching to Nginx
      1. Installation
      2. The FT Now Fails, But Show Nginx Is Running
      3. Simple Nginx Configuration
    3. Switching to Gunicorn
    4. Getting Nginx to Serve Static Files
    5. Switching to Using Unix Sockets
    6. Using Environment Variables to Adjust Settings for Production
    7. Essential Googling the Error Message
      1. Fixing ALLOWED_HOSTS with Nginx: passing on the Host header
    8. Using a .env File to Store Our Environment Variables
      1. Generating a secure SECRET_KEY
    9. Using Systemd to Make Sure Gunicorn Starts on Boot
      1. Saving Our Changes: Adding Gunicorn to Our requirements.txt
    10. Thinking About Automating
      1. Saving Templates for Our Provisioning Config Files
    11. Saving Our Progress
  17. 11. Automating Deployment with Fabric
    1. Breakdown of a Fabric Script for Our Deployment
      1. Pulling Down Our Source Code with Git
      2. Updating the Virtualenv
      3. Creating a New .env File if Necessary
      4. Updating Static Files
      5. Migrating the Database If Necessary
    2. Trying It Out
      1. Deploying to Live
    3. Provisioning: Nginx and Gunicorn Config Using sed
    4. Git Tag the Release
    5. Further Reading
      1. Automating Provisioning with Ansible
  18. 12. Splitting Our Tests into Multiple Files, and a Generic Wait Helper
    1. Start on a Validation FT: Preventing Blank Items
      1. Skipping a Test
      2. Splitting Functional Tests Out into Many Files
      3. Running a Single Test File
    2. A New Functional Test Tool: A Generic Explicit Wait Helper
    3. Finishing Off the FT
    4. Refactoring Unit Tests into Several Files
  19. 13. Validation at the Database Layer
    1. Model-Layer Validation
      1. The self.assertRaises Context Manager
      2. A Django Quirk: Model Save Doesn’t Run Validation
    2. Surfacing Model Validation Errors in the View
      1. Checking That Invalid Input Isn’t Saved to the Database
    3. Django Pattern: Processing POST Requests in the Same View as Renders the Form
      1. Refactor: Transferring the new_item Functionality into view_list
      2. Enforcing Model Validation in view_list
    4. Refactor: Removing Hardcoded URLs
      1. The {% url %} Template Tag
      2. Using get_absolute_url for Redirects
  20. 14. A Simple Form
    1. Moving Validation Logic into a Form
      1. Exploring the Forms API with a Unit Test
      2. Switching to a Django ModelForm
      3. Testing and Customising Form Validation
    2. Using the Form in Our Views
      1. Using the Form in a View with a GET Request
      2. A Big Find and Replace
    3. Using the Form in a View That Takes POST Requests
      1. Adapting the Unit Tests for the new_list View
      2. Using the Form in the View
      3. Using the Form to Display Errors in the Template
    4. Using the Form in the Other View
      1. A Helper Method for Several Short Tests
      2. An Unexpected Benefit: Free Client-Side Validation from HTML5
    5. A Pat on the Back
    6. But Have We Wasted a Lot of Time?
    7. Using the Form’s Own Save Method
  21. 15. More Advanced Forms
    1. Another FT for Duplicate Items
      1. Preventing Duplicates at the Model Layer
      2. A Little Digression on Queryset Ordering and String Representations
      3. Rewriting the Old Model Test
      4. Some Integrity Errors Do Show Up on Save
    2. Experimenting with Duplicate Item Validation at the Views Layer
    3. A More Complex Form to Handle Uniqueness Validation
    4. Using the Existing List Item Form in the List View
    5. Wrapping Up: What We’ve Learned About Testing Django
  22. 16. Dipping Our Toes, Very Tentatively, into JavaScript
    1. Starting with an FT
    2. Setting Up a Basic JavaScript Test Runner
    3. Using jQuery and the Fixtures Div
    4. Building a JavaScript Unit Test for Our Desired Functionality
    5. Fixtures, Execution Order, and Global State: Key Challenges of JS Testing
      1. console.log for Debug Printing
      2. Using an Initialize Function for More Control Over Execution Time
    6. Columbo Says: Onload Boilerplate and Namespacing
    7. JavaScript Testing in the TDD Cycle
    8. A Few Things That Didn’t Make It
  23. 17. Deploying Our New Code
    1. Staging Deploy
    2. Live Deploy
    3. What to Do If You See a Database Error
    4. Wrap-Up: git tag the New Release
  24. III. More Advanced Topics in Testing
  25. 18. User Authentication, Spiking, and De-Spiking
    1. Passwordless Auth
    2. Exploratory Coding, aka “Spiking”
      1. Starting a Branch for the Spike
      2. Frontend Log in UI
      3. Sending Emails from Django
      4. Another Secret, Another Environment Variable
      5. Storing Tokens in the Database
      6. Custom Authentication Models
      7. Finishing the Custom Django Auth
    3. De-spiking
      1. Reverting Our Spiked Code
    4. A Minimal Custom User Model
      1. Tests as Documentation
    5. A Token Model to Link Emails with a Unique ID
  26. 19. Using Mocks to Test External Dependencies or Reduce Duplication
    1. Before We Start: Getting the Basic Plumbing In
    2. Mocking Manually, aka Monkeypatching
    3. The Python Mock Library
      1. Using unittest.patch
      2. Getting the FT a Little Further Along
      3. Testing the Django Messages Framework
      4. Adding Messages to Our HTML
      5. Starting on the Login URL
      6. Checking That We Send the User a Link with a Token
    4. De-spiking Our Custom Authentication Backend
      1. 1 if = 1 More Test
      2. The get_user Method
      3. Using Our Auth Backend in the Login View
    5. An Alternative Reason to Use Mocks: Reducing Duplication
      1. Using mock.return_value
      2. Patching at the Class Level
    6. The Moment of Truth: Will the FT Pass?
    7. It Works in Theory! Does It Work in Practice?
      1. Using Our New Environment Variable, and Saving It to .env
    8. Finishing Off Our FT, Testing Logout
  27. 20. Test Fixtures and a Decorator for Explicit Waits
    1. Skipping the Login Process by Pre-creating a Session
      1. Checking That It Works
    2. Our Final Explicit Wait Helper: A Wait Decorator
  28. 21. Server-Side Debugging
    1. The Proof Is in the Pudding: Using Staging to Catch Final Bugs
    2. Inspecting Logs on the Server
    3. Another Environment Variable
    4. Adapting Our FT to Be Able to Test Real Emails via POP3
    5. Managing the Test Database on Staging
      1. A Django Management Command to Create Sessions
      2. Getting the FT to Run the Management Command on the Server
      3. Using Fabric Directly from Python
      4. Recap: Creating Sessions Locally Versus Staging
    6. Updating our Deploy Script
    7. Wrap-Up
  29. 22. Finishing “My Lists”: Outside-In TDD
    1. The Alternative: “Inside-Out”
    2. Why Prefer “Outside-In”?
    3. The FT for “My Lists”
    4. The Outside Layer: Presentation and Templates
    5. Moving Down One Layer to View Functions (the Controller)
    6. Another Pass, Outside-In
      1. A Quick Restructure of the Template Inheritance Hierarchy
      2. Designing Our API Using the Template
      3. Moving Down to the Next Layer: What the View Passes to the Template
    7. The Next “Requirement” from the Views Layer: New Lists Should Record Owner
      1. A Decision Point: Whether to Proceed to the Next Layer with a Failing Test
    8. Moving Down to the Model Layer
      1. Final Step: Feeding Through the .name API from the Template
  30. 23. Test Isolation, and “Listening to Your Tests”
    1. Revisiting Our Decision Point: The Views Layer Depends on Unwritten Models Code
    2. A First Attempt at Using Mocks for Isolation
      1. Using Mock side_effects to Check the Sequence of Events
    3. Listen to Your Tests: Ugly Tests Signal a Need to Refactor
    4. Rewriting Our Tests for the View to Be Fully Isolated
      1. Keep the Old Integrated Test Suite Around as a Sanity Check
      2. A New Test Suite with Full Isolation
      3. Thinking in Terms of Collaborators
    5. Moving Down to the Forms Layer
      1. Keep Listening to Your Tests: Removing ORM Code from Our Application
    6. Finally, Moving Down to the Models Layer
      1. Back to Views
    7. The Moment of Truth (and the Risks of Mocking)
    8. Thinking of Interactions Between Layers as “Contracts”
      1. Identifying Implicit Contracts
      2. Fixing the Oversight
    9. One More Test
    10. Tidy Up: What to Keep from Our Integrated Test Suite
      1. Removing Redundant Code at the Forms Layer
      2. Removing the Old Implementation of the View
      3. Removing Redundant Code at the Forms Layer
    11. Conclusions: When to Write Isolated Versus Integrated Tests
      1. Let Complexity Be Your Guide
      2. Should You Do Both?
      3. Onwards!
  31. 24. Continuous Integration (CI)
    1. Installing Jenkins
    2. Configuring Jenkins
      1. Initial Unlock
      2. Suggested Plugins for Now
      3. Configuring the Admin User
      4. Adding Plugins
      5. Telling Jenkins Where to Find Python 3 and Xvfb
      6. Finishing Off with HTTPS
    3. Setting Up Our Project
    4. First Build!
    5. Setting Up a Virtual Display So the FTs Can Run Headless
    6. Taking Screenshots
    7. If in Doubt, Try Bumping the Timeout!
    8. Running Our QUnit JavaScript Tests in Jenkins with PhantomJS
      1. Installing node
      2. Adding the Build Steps to Jenkins
    9. More Things to Do with a CI Server
  32. 25. The Token Social Bit, the Page Pattern, and an Exercise for the Reader
    1. An FT with Multiple Users, and addCleanup
    2. The Page Pattern
    3. Extend the FT to a Second User, and the “My Lists” Page
    4. An Exercise for the Reader
  33. 26. Fast Tests, Slow Tests, and Hot Lava
    1. Thesis: Unit Tests Are Superfast and Good Besides That
      1. Faster Tests Mean Faster Development
      2. The Holy Flow State
      3. Slow Tests Don’t Get Run as Often, Which Causes Bad Code
      4. We’re Fine Now, but Integrated Tests Get Slower Over Time
      5. Don’t Take It from Me
      6. And Unit Tests Drive Good Design
    2. The Problems with “Pure” Unit Tests
      1. Isolated Tests Can Be Harder to Read and Write
      2. Isolated Tests Don’t Automatically Test Integration
      3. Unit Tests Seldom Catch Unexpected Bugs
      4. Mocky Tests Can Become Closely Tied to Implementation
      5. But All These Problems Can Be Overcome
    3. Synthesis: What Do We Want from Our Tests, Anyway?
      1. Correctness
      2. Clean, Maintainable Code
      3. Productive Workflow
      4. Evaluate Your Tests Against the Benefits You Want from Them
    4. Architectural Solutions
      1. Ports and Adapters/Hexagonal/Clean Architecture
      2. Functional Core, Imperative Shell
    5. Conclusion
      1. Further Reading
  34. Obey the Testing Goat!
    1. Testing Is Hard
      1. Keep Your CI Builds Green
      2. Take Pride in Your Tests, as You Do in Your Code
    2. Remember to Tip the Bar Staff
    3. Don’t Be a Stranger!
  35. A. PythonAnywhere
    1. Running Firefox Selenium Sessions with Xvfb
    2. Setting Up Django as a PythonAnywhere Web App
    3. Cleaning Up /tmp
    4. Screenshots
    5. The Deployment Chapter
  36. B. Django Class-Based Views
    1. Class-Based Generic Views
    2. The Home Page as a FormView
    3. Using form_valid to Customise a CreateView
    4. A More Complex View to Handle Both Viewing and Adding to a List
      1. The Tests Guide Us, for a While
      2. Until We’re Left with Trial and Error
      3. Back on Track
      4. Is That Your Final Answer?
    5. Compare Old and New
    6. Best Practices for Unit Testing CBGVs?
      1. Take-Home: Having Multiple, Isolated View Tests with Single Assertions Helps
  37. C. Provisioning with Ansible
    1. Installing System Packages and Nginx
    2. Configuring Gunicorn, and Using Handlers to Restart Services
    3. What to Do Next
      1. Move Deployment out of Fabric and into Ansible
      2. Use Vagrant to Spin Up a Local VM
  38. D. Testing Database Migrations
    1. An Attempted Deploy to Staging
    2. Running a Test Migration Locally
      1. Entering Problematic Data
      2. Copying Test Data from the Live Site
      3. Confirming the Error
    3. Inserting a Data Migration
      1. Re-creating the Old Migration
    4. Testing the New Migrations Together
    5. Conclusions
  39. E. Behaviour-Driven Development (BDD)
    1. What Is BDD?
    2. Basic Housekeeping
    3. Writing an FT as a “Feature” Using Gherkin Syntax
      1. As-a /I want to/So that
      2. Given/When/Then
      3. Not Always a Perfect Fit!
    4. Coding the Step Functions
      1. Generating Placeholder Steps
    5. First Step Definition
    6. setUp and tearDown Equivalents in environment.py
    7. Another Run
    8. Capturing Parameters in Steps
    9. Comparing the Inline-Style FT
    10. BDD Encourages Structured Test Code
    11. The Page Pattern as an Alternative
    12. BDD Might Be Less Expressive than Inline Comments
    13. Will Nonprogrammers Write Tests?
    14. Some Tentative Conclusions
  40. F. Building a REST API: JSON, Ajax, and Mocking with JavaScript
    1. Our Approach for This Appendix
    2. Choosing Our Test Approach
    3. Basic Piping
    4. Actually Responding with Something
    5. Adding POST
    6. Testing the Client-Side Ajax with Sinon.js
      1. Sinon and Testing the Asynchronous Part of Ajax
    7. Wiring It All Up in the Template to See If It Really Works
    8. Implementing Ajax POST, Including the CSRF Token
    9. Mocking in JavaScript
      1. Finishing the Refactor: Getting the Tests to Match the Code
    10. Data Validation: An Exercise for the Reader?
  41. G. Django-Rest-Framework
    1. Installation
    2. Serializers (Well, ModelSerializers, Really)
    3. Viewsets (Well, ModelViewsets, Really) and Routers
    4. A Different URL for POST Item
    5. Adapting the Client Side
    6. What Django-Rest-Framework Gives You
      1. Configuration Instead of Code
      2. Free Functionality
  42. H. Cheat Sheet
    1. Initial Project Setup
    2. The Basic TDD Workflow
    3. Moving Beyond Dev-Only Testing
    4. General Testing Best Practices
    5. Selenium/Functional Testing Best Practices
    6. Outside-In, Test Isolation Versus Integrated Tests, and Mocking
  43. I. What to Do Next
    1. Notifications—Both on the Site and by Email
    2. Switch to Postgres
    3. Run Your Tests Against Different Browsers
    4. 404 and 500 Tests
    5. The Django Admin Site
    6. Write Some Security Tests
    7. Test for Graceful Degradation
    8. Caching and Performance Testing
    9. JavaScript MVC Frameworks
    10. Async and Websockets
    11. Switch to Using py.test
    12. Check Out coverage.py
    13. Client-Side Encryption
    14. Your Suggestion Here
  44. J. Source Code Examples
    1. Full List of Links for Each Chapter
    2. Using Git to Check Your Progress
    3. Downloading a ZIP File for a Chapter
    4. Don’t Let it Become a Crutch!
  45. Bibliography
  46. Index

Product information

  • Title: Test-Driven Development with Python, 2nd Edition
  • Author(s): Harry Percival
  • Release date: August 2017
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491958650