Book Image

Test-Driven Python Development

By : Siddharta Govindaraj
Book Image

Test-Driven Python Development

By: Siddharta Govindaraj

Overview of this book

This book starts with a look at the test-driven development process, and how it is different from the traditional way of writing code. All the concepts are presented in the context of a real application that is developed in a step-by-step manner over the course of the book. While exploring the common types of smelly code, we will go back into our example project and clean up the smells that we find. Additionally, we will use mocking to implement the parts of our example project that depend on other systems. Towards the end of the book, we'll take a look at the most common patterns and anti-patterns associated with test-driven development, including integration of test results into the development process.
Table of Contents (20 chapters)
Test-Driven Python Development
About the Author
About the Reviewers

Running the examples in this book

The code examples in this book have been written for Python 3.4. They use some syntax that is not available in older versions of Python. Therefore, there are a few places we will need to change the code if we want to run the examples in, say, Python 2.6.


The entire source code with all the changes below is available online at Get this code if you would like to run the example code in this book under Python 2.6, 2.7, 3.0, 3.1, 3.2, or 3.3

The following changes are required:

  • Enum: The Enum library is not in the standard library with older Python versions. It has been backported and can be installed from PyPi. Install the Enum34 library to use this feature.

  • set syntax: Newer versions of Python support the single curly braces shorthand syntax to create set objects like{"MSFT"}. In older versions, we will need to explicitly create sets with this equivalent longhand syntax: set(["MSFT"]).

  • print statement: print is defined as a statement in Python 2.x, so we cannot call it as a function, neither can we mock it out. We can get around this by adding the line from __future__ import print_function to the top of all the files that use print.

  • builtins: The builtins module is called __builtin__ in Python 2.x. Therefore, we need to use __builtin__.print or when we want to mock the print or open functions.

  • yield from expression: This expression is not available in older Python versions. It has to be replaced with an iteration.

  • mock_open: This mock helper only mocks the read method in the backported version. It doesn't support mocking iteration on the file object. So, we need to change the implementation to not use iteration.

With these changes, the examples in this book will work on Python 2.6 onward.