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

Pattern – spies


Mocks allow us to replace an object or class with a dummy mock object. We've seen how we can then make the mock return predefined values, so that the class under test doesn't even know that it has made a call to a mock object. However, sometimes we might want to just record that the call was made to an object, but allow the execution flow to continue to the real object and return. Such an object is known as a spy. A spy retains the functionality of recording calls and being able to assert on the calls afterwards, but it does not replace a real object like a regular mock does.

The wraps parameter when creating a mock.Mock object allows us to create spy behavior in our code. It takes an object as a value, and all calls to the mock are forwarded to the object we pass, and the return value is sent back to the caller. The following is an example:

def test_action_doesnt_fire_if_rule_doesnt_match(self):
    goog = Stock("GOOG")
    exchange = {"GOOG": goog}
    rule = PriceRule("GOOG...