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 PATH_TO_POSTS

# 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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.