Skip to content

Instantly share code, notes, and snippets.

@TimFletcher
Created February 18, 2013 22:35
Show Gist options
  • Save TimFletcher/4981397 to your computer and use it in GitHub Desktop.
Save TimFletcher/4981397 to your computer and use it in GitHub Desktop.
A fabfile for running tests in Django, note specifically the `test()` function.
from fabric.api import local
from fabric.colors import red, green
from fabric.context_managers import settings
def dumpdata():
"""
Calls dumpdata for all apps.
Remember to add new dumpdata commands for new apps here so that you always
get a full initial dump when running this task.
Is used by the ``fab rebuild`` task.
"""
local('django-admin.py dumpdata --indent 4 --natural auth --exclude auth.permission > fixtures/bootstrap_auth.json') # NOQA
local('django-admin.py dumpdata --indent 4 --natural sites > fixtures/bootstrap_sites.json') # NOQA
def loaddata():
"""
Loads the bootstrap fixtures so you can start clicking around on the site.
Is used by the ``fab rebuild`` task.
"""
local('django-admin.py loaddata fixtures/bootstrap_auth.json')
local('django-admin.py loaddata fixtures/bootstrap_sites.json')
def rebuild():
"""
Deletes the database and recreates the database and bootstrap fixtures.
"""
local('django-admin.py reset_db --router=default --noinput')
local('django-admin.py syncdb --all --noinput')
local('django-admin.py migrate --fake')
loaddata()
def test(app='', integration=1):
"""
Your central command for running tests. Call it like so:
fab test
fab test:app_name
fab test:integration=0
"""
local('clear')
command = 'django-admin.py test %s --verbosity 2 --failfast --settings=test_settings' % app
if int(integration) == 0:
command += " --exclude='integration_tests' --exclude='jasmine_tests'"
with settings(warn_only=True):
result = local(command)
if result.failed:
print red("Some tests failed", bold=True)
else:
print green("All tests passed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment