TDD with Python

Install Homebrew

We're going to start by installing Homebrew which will make installing everything else we need much easier.

Open a Terminal window and copy and paste the following into your terminal:

$ ruby -e "$(curl -fsSL"

Then ensure you have the latest version of Homebrew by typing the following:

$ brew update

And finally, ensure that Homebrew's environment is in a healthy state:

$ brew doctor

This may give you some commands to run in order to fix any problems, which you should do before continuing.

Install Python

Now we're ready to install Python on our system. We'll be using Homebrew to do that.

$ brew install python

Creating a Project

Let’s make a directory for our project:

$ mkdir piglatin-python
$ cd piglatin-python

Dependency Management

When building a software project you will often use libraries created by other people. In order to both specify and automatically download these dependencies you'll want to use a dependency management system.

Add Testing Framework

In order to express the requirements of our code we're going to use a testing framework. Testing frameworks, at their most basic, allow you to test the output of a function when given a certain input.

Lets set up dependency management for our project using pip.

Create the file requirements.txt in the root of your project with the following contents:


Then install the dependencies:

$ pip3 install -r requirements.txt

Our First Failing Test

Let's create a test for our first requirement - translating apple to appleway.

Create the Piglatin Class

Create the Translate Word Method

Fix the Method Signature

Return the Correct Answer

Another Word

Some Actual Code

Let's add some code to actually do something with the word we are passed.

The Pig Latin rules state that when the word starts with a vowel we just add `way` to the end of it.

Since we only have tests for words starting with vowels, let's do the minimum amount of work to get our test passing - appending `way` to the word that's passed it.