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 [11]:
# Make a directory called examples
#!mkdir ../examples
%cd ../examples
!ls


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

In [7]:
# Write functional_tests.py
#%%writefile functional_tests.py

from selenium import webdriver

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

assert 'Django' in browser.title


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-7-d8ffb4801ebc> in <module>()
      2 #%%writefile ../examples/functional_tests.py
      3 
----> 4 from selenium import webdriver
      5 
      6 browser = webdriver.Firefox()

ImportError: No module named 'selenium'

Installing django and selenium


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


Collecting selenium
  Downloading selenium-2.48.0.tar.gz (805kB)
    100% |████████████████████████████████| 806kB 476kB/s 
Building wheels for collected packages: selenium
  Running setup.py bdist_wheel for selenium
  Stored in directory: /home/thunder/.cache/pip/wheels/e1/34/09/4fa6eb65a3e9fded7ce3ee306ef8d398b09f35db4438567b23
Successfully built selenium
Installing collected packages: selenium
Successfully installed selenium-2.48.0

Checking that our test correctly fails


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


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/home/thunder/Documents/work/codeguild2015/code_guild/wk9/examples/functional_tests.py in <module>()
      5 browser.get('http://localhost:8000')
      6 
----> 7 assert 'Django' in browser.title

AssertionError: 

Fixing our failure


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

In [6]:
!tree ../examples/


../examples/
├── functional_tests.py
└── superlists
    ├── manage.py
    └── superlists
        ├── __init__.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

2 directories, 6 files

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


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


Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

November 13, 2015 - 20:04:01
Django version 1.8.4, using settings 'superlists.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[13/Nov/2015 20:04:03] "GET / HTTP/1.1" 200 1767
[13/Nov/2015 20:04:03] "GET /favicon.ico HTTP/1.1" 404 1940
^C

Do our tests pass now?


In [23]:
%run functional_tests.py

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


In [33]:
# 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 [34]:
%ls


db.sqlite3  functional_tests.py  manage.py*  superlists/

In [36]:
# 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"