Note: This project is currently in a work in progress so not all languages or sections are complete.

Test Driven Development with Python

Installing Homebrew

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

Open a Terminal window and run the following commands:

1
2
xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing Python

Now we can easily install Python with Homebrew using the following:

1
brew install python

Creating the Project

Now we'll use PyScaffold to initialize a project for us.

1
2
3
pip2 install pyscaffold
putup ythonpay
cd ythonpay

Dependency Management

When building software you will frequently use libraries and tools from a third party. Since they have their own development lifecycles and versions we want to be able to define a specific version of that dependency to use in our software. We want to do that with a tool and a file.

For Python we'll use the tool pip and our dependencies will be defined in the file requirements.txt.

1
# pip is installed with Python

Adding a Testing Library

We're going to use the testing library pytest to write our tests.

Edit the file requirements.txt and add pytest as a testing dependency:

1

Then we retrieve pytest by running pip2 install -r requirements.txt.

Finally we initialize pytest by running .

The First Test

Create the file tests/test_skeleton.py with the following contents:

1
2
3
4
5
import pytest
from ythonpay.skeleton import translate_word

def test_translate_word():
    assert translate_word("apple") == "appleway"

Then run the test with the command python setup.py test to see the results:

1
2
3
NameError: global name 'translate_word' is not defined

tests/test_skeleton.py:8: NameError

Comments