Wk9.0

Ch. 1 Getting Django Set Up Using a Functional Test

Obey the testing goat! Do nothing until you have a test

Before even installing anything, we'll write a test.

Writing our first test


In [1]:
# Make a directory called examples
#!mkdir ../examples
%cd ../examples
!ls


/home/thunder/Documents/work/codeguild2015/code_guild/wk9/examples
functional_tests.py  superlists

Write functional test


In [2]:
#%%writefile functional_tests.py

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://localhost:8000')

assert 'Django' in browser.title

Installing django and selenium


In [9]:
# Create a virtual env to load with selenium and django
#!conda create -yn django_class django python=3 # y flag automatically selects yes to install
!source activate django_class # activate virtual environment
!pip install --upgrade selenium # install selenium.


/bin/sh: 1: source: not found
Requirement already up-to-date: selenium in /home/thunder/.linuxbrew/lib/python2.7/site-packages

Checking that our test correctly fails


In [3]:
# Try running our tests. We're expecting an assertion error here.
%run functional_tests.py

Fixing our failure


In [10]:
!ls


functional_tests.py

In [11]:
# Use django to create a project called 'superlists'
django-admin.py startproject superlists


  File "<ipython-input-11-d058b3ea5042>", line 2
    django-admin.py startproject superlists
                               ^
SyntaxError: invalid syntax

In [ ]:
!tree ../examples/

Let's fire up our new project on a django server


In [ ]:
!cd superlists/ && python3 manage.py runserver

Do our tests pass now?


In [ ]:
%run functional_tests.py

Now that our test passed, let's turn this into a git repo.


In [ ]:
# First, move our tests into the main project dir.
#!mv functional_tests.py superlists/
# 
# Change directories and initialize our superlist into a new git repo
#%cd superlists/
#!git init .

In [ ]:
%ls

In [ ]:
# Don't add the database to git.
#! echo "db.sqlite3" >> .gitignore # >> means concatenate to end of file.

# Don't add .pyc files
#!echo "*.pyc" >> .gitignore

In [ ]:
# Add everything else.
#!git add .
#!git status
#!git commit -m "Initial commit"