1. David Chambers
  2. Mango


Mango / tests.py

# This code is taken from http://stackoverflow.com/questions/1615406/configure-django-to-find-all-doctests-in-all-modules

import doctest
import imp
import os
import re
import settings
import sys
import unittest

import mango
from mango.settings import DOCUMENTS_PATH

# Django already runs these, don't include them again
ALREADY_RUN = ['tests.py', 'models.py']

def find_untested_modules(package):
    """ Gets all modules not already included in Django's test suite """
    files = []
    for f in os.listdir(os.path.dirname(package.__file__)):
        if f.endswith('.py') and os.path.basename(f) not in ALREADY_RUN:
            files.append(re.sub(r'\.py$', '', f))
    return [imp.load_module(f, *imp.find_module(f, package.__path__)) for f in files]

def modules_callables(module):
    return [m for m in dir(module) if callable(getattr(module, m))]

def has_doctest(docstring):
    return '>>>' in docstring

__test__ = {}
for module in find_untested_modules(mango):
    for method in modules_callables(module):
        docstring = str(getattr(module, method).__doc__)
        if has_doctest(docstring):
            print 'Mango found doctest: ' + module.__name__ + '.' + method

            # import the method itself, so doctest can find it
            _temp = __import__(module.__name__, globals(), locals(), [method])
            locals()[method] = getattr(_temp, method)

            # Django looks in __test__ for doctests to run
            __test__[method] = getattr(module, method)