Django Test-Driven Development (Python)

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

This book is for Django developers with little or no knowledge of test-driven development or testing in general. Familiarity with the command line, setting up a Python virtual environment, and starting a Django project are assumed.

Author(s): Kevin Harvey
Year: 2015

Language: English
Pages: 186

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