Book Image

Test-Driven Development with Django

By : Kevin Harvey
Book Image

Test-Driven Development with Django

By: Kevin Harvey

Overview of this book

<p>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.</p> <p>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.</p>
Table of Contents (15 chapters)
Test-Driven Development with Django
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Writing software is hard. Even the smallest projects have many moving parts. We developers are not only expected to get those parts moving, but keep them moving as the application changes over time. Test-Driven Development (TDD) is a methodology that allows us to quantify the successful function of each of these parts before we attempt to code them. Using TDD, we can focus on a single part of the application at a time, leaving a trail of tests that guard against regression as we continue to update the application.

Django is a popular web framework written in Python. Its batteries-included: the framework itself includes URL routing, object-relational mapping, templates, and many other necessities for building a modern web application. This book will take you through the process of developing a Django app by writing failing tests first, then writing application code to make those tests pass.

What this book covers

Chapter 1, Keeping Your Promises, describes the benefits of TDD in comparison to other styles of programming. We'll look at a very simple example, and talk about testing as a pillar of professional software development.

Chapter 2, Your First Test-Driven Application, introduces the example application that we'll be building throughout the book. We'll translate a user story into a browser-based functional test using Selenium, and write unit tests and application code to start to fulfill that user story.

Chapter 3, Ironclad Code, continues where the previous chapter left off, digging deeper into the API available for writing unit tests in Django. We'll cover the Python Debugger and tools for simulating web requests.

Chapter 4, Building Out and Refactoring, adds new features to the application. We'll use our test suite to maintain existing functionality while refactoring to keep our code tidy and maintainable.

Chapter 5, User Stories As Code, focuses on writing functional tests and the packages available to drive the browser during a test run. We'll learn how to select and click on elements, submit forms, switch between open windows, and perform other user actions in our UI.

Chapter 6, No App Is an Island, applies the TDD methodology to third-party API integration. We'll learn when, why, and how to mock out HTTP requests inside a single unit test so that our tests aren't relying on an outside resource (even if our app is).

Chapter 7, Share and Share Alike, introduces Django REST Framework—a tool for building a REST API with Django. We'll cover the importance of documentation when writing an API, and use the framework's tools to send requests to the API during tests.

Chapter 8, Promises Kept, takes a look back at what we've learned, and whether we've realized all the benefits from the first chapter. We'll get suggestions for next steps in TDD, and talk about some of the common pitfalls you may encounter.

What you need for this book

In order to follow along with the examples in this book, you'll need a computer with a command line interface (the default is Terminal for Linux and Mac, and Command Prompt for Windows) with Python installed. You will also need an Internet connection to download third-party packages and a text editor for writing Python code.

Who this book is for

This book is for Django developers who want to learn about TDD, and how it might help them in their work. It's expected that the reader is comfortable with reading and writing Python and has some familiarity with the Django framework.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "If you're making HTTP calls, the requests package is a great option."

A block of code is set as follows:

# multiplicator.py
defmultiplicator(x, y):
    pass

Any command-line input or output is written as follows:

$ python
>>> from multiplicator import multiplicator
>>>multiplicator(2, 3)
6

New terms and important words are shown in bold, like this: " A Continuous Integration (CI) server, for our purposes, can pull our project from version control "

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

The source code for the sample application is available on GitHub at https://github.com/kevinharvey/jmad on the test-driven-django-development branch (the repository's default). Development milestones in the book have been tagged with names like ch5-2-staff-login to make navigating the repository easier.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.