Create a gist now

Instantly share code, notes, and snippets.

Full stack BDD testing with Behave+Mechanize+Django
Feature: Demonstrate how to use the mechanize browser to do useful things.
Scenario: Logging in to our new Django site
Given a user
When I log in
Then I see my account summary
And I see a warm and welcoming message
Scenario: Loggout out of our new Django site
Given a user
When I log in
And I log out
Then I see a cold and heartless message
# -*- coding: utf-8 -*-
"""steps/ -- step implementation for our browser feature demonstration.
from behave import given, when, then
@given('a user')
def step(context):
from django.contrib.auth.models import User
u = User(username='foo', email='')
@when('I log in')
def step(context):
br = context.browser'/account/login/'))
br.form['username'] = 'foo'
br.form['password'] = 'bar'
@then('I see my account summary')
def step(context):
br = context.browser
response = br.response()
assert response.code == 200
assert br.geturl().endswith('/account/'), br.geturl()
@then('I see a warm and welcoming message')
def step(context):
# Remember, context.parse_soup() parses the current response in
# the mechanize browser.
soup = context.parse_soup()
msg = str(soup.findAll('h2', attrs={'class': 'welcome'})[0])
assert "Welcome, foo!" in msg
# -*- coding: utf-8 -*-
"""environment -- environmental setup for Django+Behave+Mechanize
This should go in features/
For the basic solution:
import os
# This is necessary for all installed apps to be recognized, for some reason.
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
def before_all(context):
# Even though DJANGO_SETTINGS_MODULE is set, this may still be
# necessary. Or it may be simple CYA insurance.
from import setup_environ
from myproject import settings
from django.test import utils
## If you use South for migrations, uncomment this to monkeypatch
## syncdb to get migrations to run.
# from import patch_for_test_db_setup
# patch_for_test_db_setup()
### Set up the WSGI intercept "port".
import wsgi_intercept
from django.core.handlers.wsgi import WSGIHandler
host = = 'localhost'
port = context.port = getattr(settings, 'TESTING_MECHANIZE_INTERCEPT_PORT', 17681)
# NOTE: Nothing is actually listening on this port. wsgi_intercept
# monkeypatches the networking internals to use a fake socket when
# connecting to this port.
wsgi_intercept.add_wsgi_intercept(host, port, WSGIHandler)
import urlparse
def browser_url(url):
"""Create a URL for the virtual WSGI server.
e.g context.browser_url('/'), context.browser_url(reverse('my_view'))
return urlparse.urljoin('http://%s:%d/' % (host, port), url)
context.browser_url = browser_url
### BeautifulSoup is handy to have nearby. (Substitute lxml or html5lib as you see fit)
from BeautifulSoup import BeautifulSoup
def parse_soup():
"""Use BeautifulSoup to parse the current response and return the DOM tree.
r = context.browser.response()
html =
return BeautifulSoup(html)
context.parse_soup = parse_soup
def before_scenario(context, scenario):
# Set up the scenario test environment
# We must set up and tear down the entire database between
# scenarios. We can't just use db transactions, as Django's
# TestClient does, if we're doing full-stack tests with Mechanize,
# because Django closes the db connection after finishing the HTTP
# response.
from django.db import connection
connection.creation.create_test_db(verbosity=1, autoclobber=True)
### Set up the Mechanize browser.
from wsgi_intercept import mechanize_intercept
# MAGIC: All requests made by this monkeypatched browser to the magic
# host and port will be intercepted by wsgi_intercept via a
# fake socket and routed to Django's WSGI interface.
browser = context.browser = mechanize_intercept.Browser()
def after_scenario(context, scenario):
# Tear down the scenario test environment.
from django.db import connection
connection.creation.destroy_test_db(verbosity=1, autoclobber=True)
# Bob's your uncle.
def after_all(context):
from django.test import utils
<html> <head>
<h2 class="welcome">Welcome, foo!</h2>
</body> </html>
I, David Eyk, hereby dedicate this work to the public domain by waiving all of my rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law.
You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission. See for a full summary and legal text.
<html> <head>
<form action="." method="post">
{% csrf_token %}
<input name="username" />
<input name="password" />
<input type="submit" value="Submit">
</body> </html>
# Django settings for myproject project.
import os
DEBUG = True
# ('Your Name', ''),
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'myproject', # Or path to database file if using sqlite3.
'USER': 'myuser', # Not used with sqlite3.
'PASSWORD': 'mypassword', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
# Local time zone for this installation. Choices can be found here:
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Chicago'
# Language code for this installation. All choices can be found here:
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True
# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/"
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "", ""
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/"
# URL prefix for static files.
# Example: ""
STATIC_URL = '/static/'
# Additional locations of static files
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
# List of finder classes that know how to find static files in
# various locations.
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
LOGIN_REDIRECT_URL = '/account/'
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'gz6b+p%k*_lty%v#8g%-70ndzd@h&amp;v_tp&amp;s)78s%$m)qlc=06b'
# List of callables that know how to import templates from various sources.
# 'django.template.loaders.eggs.Loader',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
ROOT_URLCONF = 'myproject.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'myproject.wsgi.application'
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates'),
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
url(r'account/', include('django.contrib.auth.urls')),
url('^account/$', direct_to_template, {
'template': 'home.html'
}, name='home')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment