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

Exploring the Rule classes

So far we have concentrated on the Stock class. Let us now turn our attention to the rule classes.


From this point in the book, we will look at the implementation code, and then show how we can test it effectively. Note, however, that this does not mean writing the code first, followed by the unit test. The TDD process is still test first, followed by the implementation. It is the test case which will drive the implementation strategy. We are showing the implementation code first, only because it makes it easier to understand the testing concepts that will follow. All this code was originally written test first!

The rule classes keep track of the rules that the user wants to track and they can be of different types. For example, send an alert when a stock crosses a value, or matches a trend.

Here is an example of a PriceRule implementation:

class PriceRule:
        """PriceRule is a rule that triggers when a stock price
        satisfies a condition (usually greater...