Test Driven Development with Python

My notes from reading the book Test Driven Development with Python

by Harry Percival

Functional tests should help you build an applicaiton with the right functionality, and guarantee you never
accidentally break it. Unit tests should help you to write code that's clean and bug free.

Setting up the virtualenv

Install virtualenv

pip install --user virtualenvwrapper
echo "source virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc

Create a virtualenv called environment-name

mkvirtualenv --python=python3 environment-name

Activate the environment

workon environment-name
  • to deactivate, $ deactivate

Check which Python

which python
python --version

Install Django and Selenium

pip install "django<1.11" "selenium<3"

Get Django Up and Running

  • Create Project and Start Server
django-admin.py startproject project-name
cd project-name
python manage.py runserver

Start a Git Repository

cd project-name
git init .
echo "db.sqlite3" >> .gitignore
echo "__pycache__" >> .gitignore
echo "\*.pyc" >> .gitignore
git status
git add --all
git commit -a

Create an Application

cd project-name
python manage.py startapp app-name

Create a Unit Test

vim app-name/tests.py

Django's MVC, URLs and View Functions

Django's main job is to do something when a user asks for a URL. Django's workflow:

  1. HTTP request comes in for a URL
  2. Django's rules decide which view function handles the request (resolving the URL)
  3. View function processes the request and returns an HTTP response

urls.py

Django uses a file urls.py to define how URLs map to view functions --- located in project-name/project-name folder

  • Add url(r'^$', views.home_page, name='home'); to urls.py

Useful Commands and Concepts

Run the Django dev server

python manage.py runserver

Run the functional tests

python manage.py test functional_tests

Running the unit tests

python manage.py test app-name

The unit-test/code cycle

  1. Run the unit tests in the terminal.
  2. Make a minimal code change in the editor.
  3. Repeat!

Don't Test Constants Rule, and Templates

Unit tests are about testing logic, flow control, and configuration
Templates are a very powerful feature of Django. Strength consists of substituting Python variables
HTML text
Django requires all applications to be registered within the INSTALLED_APPS definition inside settings.py

Django ORM

An Object-Relational Mapper (ORM) is a layer of abstraction for ata stored in a database with tables, rows, and
columns. It lets us work with databases using familiar object-oriented metaphors which work well with code. Classes map
to database tables, attributes map to columns, and an individual instance of a class represents a row of data in the
database

Database Migration

In Django, the ORM's job is to model the database, but there's a second system that's in charge of building the database
called migrations. Its job is to add and remove tables and columns, based on changes you make to models.py

How to build view a first database migration

  • $ python manage.py makemigrations
  • $ ls app-name/migrations

Random Notes from Book

  1. only methods that begin with test_ will get run as tests, other methods can be used for other purposes
Test Driven Development with Python
Share this