Created
June 9, 2015 21:43
-
-
Save purple4reina/10fdce089b3649669d05 to your computer and use it in GitHub Desktop.
Running Django Unit Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Run all unit tests within the current working directory optionally skipping | |
some modules | |
This script should be used instead of running `./manage.py test`. This django | |
provided unittest runner does not reset the testing database between each | |
testing file. The only way to ensure the testing database is completely dropped | |
is by running each test file individually. | |
""" | |
import argparse | |
import os | |
import subprocess | |
import sys | |
os.environ["DJANGO_SETTINGS_MODULE"] = "settings" | |
my_dir = os.path.dirname(sys.argv[0]) | |
additional_paths = [my_dir, "../", "."] | |
for path in additional_paths: | |
sys.path.insert(0, path) | |
from settings import ROOTDIR | |
PASS = '\033[92m' | |
FAIL = '\033[91m' | |
STOP = '\033[0m' | |
def get_all_test_modules(): | |
pwd = os.getcwd() | |
my_cmd = ["find", pwd, "-name", "test_*.py"] | |
proc = subprocess.Popen(my_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
out, _ = proc.communicate() | |
test_module_paths = out.split('\n')[:-1] | |
test_modules = [] | |
for path in test_module_paths: | |
if not path.startswith(ROOTDIR): | |
continue | |
path = path.split(ROOTDIR)[-1] | |
path = path[1:-3] # strip begining / and ending .py | |
path = path.replace('/', '.') # turn the file path to a module path | |
test_modules.append(path) | |
test_modules.sort() | |
return test_modules | |
def run_unittests(test_module): | |
os.chdir(ROOTDIR) | |
my_cmd = ["python", "manage.py", "test", test_module, "--failfast"] | |
proc = subprocess.Popen(my_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
out, err = proc.communicate() | |
if proc.returncode: | |
print FAIL, "[[FAIL]]", STOP | |
return test_module | |
else: | |
print PASS, "[[PASS]]", STOP | |
return | |
def main(test_modules, skip=None): | |
failures = [] | |
for test_module in test_modules: | |
if any(test_module.startswith(mod) for mod in skip): | |
continue | |
sys.stdout.write("Running tests for {}".format(test_module).ljust(60)) | |
sys.stdout.flush() | |
failed_module = run_unittests(test_module) | |
if failed_module: | |
failures.append(failed_module) | |
return failures | |
def get_modules_to_skip(): | |
parser = argparse.ArgumentParser(description='Run django unittests') | |
parser.add_argument( | |
'--skip', | |
metavar='module', | |
type=str, | |
nargs='+', | |
help='test modules to be skipped' | |
) | |
args = parser.parse_args() | |
return args.skip if args.skip else [] | |
if __name__ == '__main__': | |
skip_mods = get_modules_to_skip() | |
test_modules = get_all_test_modules() | |
failures = main(test_modules, skip=skip_mods) | |
if failures: | |
print "\n{} of {} unit test modules ended in failure:".format( | |
len(failures), len(test_modules)) | |
for failure in failures: | |
print " - {}".format(failure) | |
sys.exit(len(failures)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment