Skip to content

Instantly share code, notes, and snippets.

@pvieytes
Last active December 16, 2015 10:30
Show Gist options
  • Save pvieytes/6333b607c1e0cd405ff0 to your computer and use it in GitHub Desktop.
Save pvieytes/6333b607c1e0cd405ff0 to your computer and use it in GitHub Desktop.
Django (1.6) functional tests (selenium + manage.py runserver)

Django functional testing

Django manage.py custom command for functional testing.

Fisrtly, the command starts the django sever via python manage.py runserver and secondly, it runs some Selenium tests.

Use:

python manage.py run_functional_tests --settings=config.settings.test

# file path: functional_tests/functional_tests.py
import time
from selenium import webdriver
def run():
browser = webdriver.Firefox()
browser.get('http://localhost:8000')
for i in xrange(0,11):
time.sleep(1)
print 'sleep {}s.'.format(i)
browser.quit()
#file path: functional_tests/management/commands/run_functional_tests.py
import os
import sys
import subprocess
import time
from django.core.management.base import BaseCommand
from functional_tests.functional_tests import run as run_functional
class Command(BaseCommand):
help = "Running Tests"
def handle(self, *args, **options):
managePath= '/path/to/manage.py'
max_waiting_time = 60
time_step = 5
time_counter = 0
print "Running Tests"
# start django server
print "Starting Django Test Server"
django = subprocess.Popen(['python', managePath, 'runserver', '--noreload'])
print "Waiting for Django to Start"
django_reachable = False
while not django_reachable:
wget = subprocess.call (['wget',
'--output-file=/dev/null',
'--spider',
'http://localhost:8000'])
if wget == 0:
print "Django started."
django_reachable = True
else:
if time_counter > max_waiting_time:
django.kill()
sys.exit("* ERROR: django timeout *")
print "Still waiting ..."
time.sleep(time_step)
time_counter += time_step
run_functional()
print "kill django "
django.kill()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment