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
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Running tests from the command line


Throughout the book, we have used the following syntax to run our tests:

python.exe -m unittest

The ability to directly run a module with the -m flag was only introduced with Python 2.7. This syntax will not work if we are using an older version of Python. Instead, the unittest2 module from PyPi contains a unit2 script that mimics this behavior. The command line parameters remain the same, so we get the following the command:

python3 -m unittest discover -s stock_alerter -t .

And the above command now becomes:

unit2 discover -s stock_alerter -t .

If we use a build tool, it becomes fairly simple to check the version of Python and execute the appropriate command, thereby allowing the developer to run the tests in a uniform way, irrespective of the Python version being used.

With these changes in place, we will be able to use all the features described in this book, while being able to support Python 2.x and Python 3.x uniformly.