Skip to Content
Learning Test-Driven Development
book

Learning Test-Driven Development

by Saleem Siddiqui
October 2021
Intermediate to advanced
277 pages
5h 48m
English
O'Reilly Media, Inc.
Content preview from Learning Test-Driven Development

Chapter 7. Modules in Python

A module is a file containing Python definitions and statements.

The Python Tutorial

In this chapter, we’ll do a few things to improve the organization of our Python code. We’ll separate our test code from our production code using modules. We’ll see how the scoping and import rules in Python help us ensure the dependencies in our code are correct. Finally, we’ll remove a redundant test from our code, making things compact and meaningful.

Separating Our Code into Modules

We have production code for Money and Portfolio right next to our test code in the same file. We need to separate this code into individual source files.

Let’s first create two new files named money.py and portfolio.py in the same folder as test_money.py. Our folder structure looks like this:

py
├── money.py
├── portfolio.py
└── test_money.py

We move the code for Money and Portfolio classes to money.py and portfolio.py, respectively. This code segment shows the complete contents of portfolio.py after this code relocation:

import functools
import operator

class Portfolio:
    def __init__(self):
        self.moneys = []

    def add(self, *moneys):
        self.moneys.extend(moneys)

    def evaluate(self, currency):
        total = functools.reduce(operator.add,
                                 map(lambda m: m.amount, self.moneys), 0)
        return Money(total, currency)

Notice that we carry the two import statements along with the code for the Portfolio class, because Portfolio uses functools and operator.

The file money.py, not shown here, similarly contains ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Domain-Driven Design

Learning Domain-Driven Design

Vlad Khononov
Learning Go

Learning Go

Jon Bodner
Learning React, 2nd Edition

Learning React, 2nd Edition

Alex Banks, Eve Porcello

Publisher Resources

ISBN: 9781098106461Errata Page