Skip to content

Instantly share code, notes, and snippets.

@mcbhenwood
Created November 8, 2012 15:43
Show Gist options
  • Save mcbhenwood/4039572 to your computer and use it in GitHub Desktop.
Save mcbhenwood/4039572 to your computer and use it in GitHub Desktop.
Run Selenium tests against the django test server, from command line with no GUI
"""
Run Selenium headless tests against Django test server
------------------------------------------------------
Creates and destroys a test environment each time.
Requires Xvfb and IceWeasel to be installed. For Debian flavours:
sudo apt-get install xvfb iceweasel
For details, see:
http://pyuseful.wordpress.com/2012/11/08/running-cucumber-style-tests-in-django-using-behave/
"""
import os
from os.path import abspath, join, split
import subprocess
import sys
import time
from behave.__main__ import main as behave_main
DJANGO_PROJECT_DIR = 'djangoproject' # Substitute your top level Django project folder name
if __name__ == '__main__':
project_dir = split(abspath(__file__))[0]
sys.path.insert(0, join(project_dir, DJANGO_PROJECT_DIR))
def run():
print('Starting django testserver')
django_process = subprocess.Popen(
['python', join(DJANGO_PROJECT_DIR, 'manage.py'), 'testserver', '--noinput'],
stdout=open('/dev/null', 'w'),
stderr=open('/dev/null', 'w'))
time.sleep(2)
print('Starting Xvfb')
xvfb_process = subprocess.Popen(
['Xvfb', ':99', '-ac', '-screen', '0', '1024x768x8'])
os.environ["DISPLAY"] = ":99"
try:
behave_main() # This calls sys.exit() under all circumstances
finally:
print('Stopping django testserver')
django_process.terminate()
print('Stopping Xvfb')
xvfb_process.terminate()
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment