Skip to content

Instantly share code, notes, and snippets.

@winhamwr
Created March 28, 2011 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winhamwr/890975 to your computer and use it in GitHub Desktop.
Save winhamwr/890975 to your computer and use it in GitHub Desktop.
Example file for simplified running of nosedjango tests for a project
#! /usr/bin/env python
# Example: $ ./run_tests.py --django-sqlite --multiprocess
import os
import subprocess
import sys
scripts_dir = os.path.abspath(os.path.dirname(__file__))
pstat_dir = os.path.join(scripts_dir, '..', 'pstat')
os.chdir(pstat_dir)
sys.path.append(pstat_dir)
try:
from nose.core import TestProgram
except ImportError:
print 'nose is required to run the pstat test suite'
sys.exit(1)
try:
import pstat
except ImportError:
print "Can't find pstat to test: %s" % sys.exc_info()[1]
sys.exit(1)
else:
print "Pstat %s test suite running (Python %s)..." % (
pstat.VERSION, sys.version.split()[0])
covered_modules = ['pstat', 'durationfield.tests', 'mailer']
default_modules = ['pstat', 'mailer']
coverage_opts = ['--with-coverage', '--cover-erase']
for m in covered_modules:
coverage_opts.extend(['--cover-package', str(m)])
excluded_tests = ['test_runner.*',
'selenium.*', 'django_assets.*', 'pstat_deploy.*',
'selenium_tests.*']
exclusion_opts = []
for t in excluded_tests:
exclusion_opts.extend(['--exclude', str(t)])
# Need to set the environ because TestProgram ignores the verbosity arguments
os.environ['NOSE_VERBOSE'] = '2'
opts = [
'--verbosity=2',
'--with-id',
'--with-xunit',
'--with-doctest',
'--with-django', '--django-settings', 'pstat.settings', # nosedjango
'--with-django-sphinxsearch', '--sphinx-config-tpl',
os.path.join(scripts_dir, 'sphinx_test.conf'),
'--with-django-testfs',
'--with-django-celery',
'--with-pstat',
]
if '--coverage' in sys.argv:
sys.argv.remove('--coverage')
else:
coverage_opts = []
if '--django-sqlite' in sys.argv:
sys.argv.remove('--django-sqlite')
opts.append('--with-django-sqlite')
elif '--with-django-sqlite' in sys.argv:
sys.argv.remove('--with-django-sqlite')
opts.append('--with-django-sqlite')
if '--failed' in sys.argv:
sys.argv.remove('--failed')
opts.insert(1, '--failed')
if '--no-testfs' in sys.argv:
sys.argv.remove('--no-testfs')
opts.remove('--with-django-testfs')
if '--multiprocess' in sys.argv:
if not '--with-django-sqlite' in opts:
raise Exception("--multiprocess must be used with the --django-sqlite argument")
sys.argv.remove('--multiprocess')
import multiprocessing
opts.append('--processes')
num_processes = multiprocessing.cpu_count()
if num_processes == 1:
num_processes = 2 # We want at least 2 processes, even on a single core
opts.append(str(num_processes))
# multiprocess doesn't play nice with the testId plugin
opts.remove('--with-id')
# If the user didn't specify any modules on the command line, run the default
# modules.
user_args = sys.argv[1:]
args = opts + coverage_opts + user_args
if len([a for a in user_args if not a.startswith('-')]) == 0:
args += exclusion_opts + default_modules
cmd_string = ' '.join(['nosetests'] + args)
print "Running: [%s]" % cmd_string
TestProgram(argv=args, exit=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment