Test-Driven Development with Django

Book Description

Develop powerful, fully-featured Django applications by writing tests first

In Detail

Test-Driven Development (TDD) simplifies the trickiest of software tasks with its unique ability to peel back problems into layers. The testing tools available in Python and Django make test writing a joy, and the full coverage test suite that results from TDD is a boon to any project.

This guide to developing with Django takes a test-first approach: write a test, then write enough production code to get it to pass. You'll quickly get hands-on experience, writing tests for a database-driven application with the TDD methodology. Use this book to build the skills and habits that make testing a regular part of your workflow.

What You Will Learn

  • Codify user stories as browser-based tests to ensure their completion
  • Write isolated unit tests that not only confirm your application, but also explain it
  • Use the red-green-refactor TDD cycle to create and refine your code by changing tests first
  • Test integrations with external APIs by testing their documentation
  • Mock out calls to external services and internal functions
  • Explore the basics of documentation-driven API design
  • Other testing tools available in popular Python packages such as Django REST framework and VCR.py

Publisher Resources

Download Example Code

Table of Contents

  1. Test-Driven Development with Django
    1. Table of Contents
    2. Test-Driven Development with Django
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Keeping Your Promises
      1. What is Test-Driven Development?
        1. A simple example
        2. Red/green/refactor
        3. Testing is a pillar of professional software development
          1. Version control
          2. Documentation
          3. Testing
          4. Continuous Integration
      2. How does TDD help in building better software?
      3. Doesn't it take longer?
      4. Can't I just write the tests later?
      5. Summary
    9. 2. Your First Test-Driven Application
      1. Where do we begin?
      2. Functional versus unit tests
        1. User versus developer experience
        2. Size
        3. Breadth
      3. The project – jmad.us
      4. Setting up your environment
      5. Starting the project
        1. Getting the sample code
        2. Starting a functional test
        3. Introducing LiveServerTestCase
        4. Introducing Selenium
        5. Fleshing out the user story
        6. Getting the test to fail
        7. Opening a page with WebDriver
        8. Finding elements with WebDriver
        9. Reading test output
      6. Setting up a Django app for unit tests
        1. Planning our unit tests
      7. Finally writing application code
        1. Testing views with RequestFactory
        2. Continuing through the functional test
        3. Adding search to the view
      8. Summary
    10. 3. Ironclad Code
      1. Using the Python Debugger in tests
      2. Using RequestFactory
        1. Running a single test with dot notation
      3. Managing test data
      4. Refactoring
        1. setUpClass versus setUp
      5. DRY testing
      6. RequestFactory versus TestClient
        1. Testing URLs
        2. Does that seem like overkill?
        3. Displaying search results
        4. Selenium tail-chasing
        5. Building the Solo detail page
      7. Summary
    11. 4. Building Out and Refactoring
      1. Improving the application
      2. A new view
      3. Pretty URLs
      4. Back into the TDD cycle
      5. Skipping ahead a bit
      6. Starting a new app
      7. Much ado about migrations
      8. Updating the functional test
      9. Refactoring the index view
      10. Summary
    12. 5. User Stories As Code
      1. A second user story
        1. Activating the Django admin site
        2. Signing in with Selenium
        3. Configuring the Django admin model list display
      2. Adding content via the Django admin
      3. Finishing up
      4. Summary
    13. 6. No App Is an Island
      1. What is a mock?
      2. MusicBrainz
        1. Digging around in the MusicBrainz sandbox
      3. Using the API in our application
      4. Mocking – when and why
        1. Adding the MB API to search
        2. Encapsulating the API call
        3. Our first mock
        4. Hacking what's returned from a mocked method
        5. Implementing the API calls
        6. Moving back up the chain
        7. Dealing with change
        8. This is just one method
      5. Summary
    14. 7. Share and Share Alike
      1. It's an API world, we're just coding in it
        1. Deceptive simplicity
          1. Writing documentation first
          2. Documentation-Driven testing
          3. More developers, more problems
        2. Building an API with Django REST framework
        3. Initial documentation
      2. Introducing Django REST framework
        1. Writing tests for API endpoints
        2. API routing with DRF's SimpleRouter
        3. Automatic APIs with DRF viewsets
        4. Converting Django models with serializers
        5. Finishing up with RetrieveModelMixin
      3. Adding data via the API
        1. POSTing data in a test
        2. Validating inbound data with a serializer
      4. Summary
    15. 8. Promises Kept
      1. How far we've come
        1. So, how did it go?
          1. We kept ourselves on track
          2. Our application can check itself
          3. We kept our thinking clear
          4. Our code is testable
          5. We can take big risks
          6. We look like pros
          7. Did we do more work than necessary?
          8. Did it take longer than it should have?
        2. What haven't we done?
          1. Production optimization
          2. An actual frontend design
          3. Quality assurance
          4. Load testing
        3. What shall we build next?
          1. Authentication for creating solos
          2. Tagging solos
          3. Haystack search
          4. Celery for caching MusicBrainz data
          5. An errant data interaction for MusicBrainz
          6. Exposing API documentation with Django REST framework
      2. To tend or not to tend?
      3. Fight through the gotchas
        1. "No module named" errors when using dotted paths to tests
        2. When error messages get weird, Google is your friend
      4. Thanks!
    16. Index

Product Information

  • Title: Test-Driven Development with Django
  • Author(s): Kevin Harvey
  • Release date: July 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781785281167