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

Test failures


Let us now see what a test failure looks like. The following is a doctest for the is_increasing_trend method:

    def is_increasing_trend(self):
        """Returns True if the past three values have been strictly
        increasing

        Returns False if there have been less than three updates so far

        >>> stock = Stock("GOOG")
        >>> stock.is_increasing_trend()
        False
        """

        return self.history[-3].value < \
            self.history[-2].value < self.history[-1].value

The following is what we get when we run the test:

Failed example:
    stock.is_increasing_trend()
Exception raised:
    Traceback (most recent call last):
      File "C:\Python34\lib\doctest.py", line 1324, in __run
        compileflags, 1), test.globs)
      File "<doctest __main__.Stock.is_increasing_trend[1]>", line 1, in <module>
        stock.is_increasing_trend()
      File "c:\Projects\tdd_with_python\src\stock_alerter\stock.py", line...