Created
June 20, 2015 14:03
-
-
Save spectras/47902eb9344f2e9121f4 to your computer and use it in GitHub Desktop.
Adding additional paths to Django's test runner
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
from django.conf import settings | |
from django.test.runner import DiscoverRunner | |
import os | |
# You'll need to adjust those | |
SUBPATHS = ('myproject/apps', 'myproject/libs') | |
ROOT = dirname(dirname(dirname(os.path.abspath(__file__)))) | |
class TestRunner(DiscoverRunner): | |
""" Adds submodules from not-importable folders to the test suite. | |
Works by adding those to the label list if building the suite | |
of the whole project. | |
""" | |
def build_suite(self, test_labels=None, extra_tests=None, **kwargs): | |
test_labels, new_labels = test_labels or ['.'], [] | |
for label in test_labels: | |
if label in ('myproject', '.'): | |
new_labels.extend(self._generate_labels()) | |
else: | |
new_labels.append(label) | |
return super(TestRunner, self).build_suite(new_labels, extra_tests, **kwargs) | |
def _generate_labels(self): | |
""" Generate paths for all the modules we inject """ | |
dirname = os.path.dirname | |
for subpath in SUBPATHS: | |
path = os.path.join(ROOT, subpath) | |
for item in os.listdir(path): | |
itempath = os.path.join(path, item) | |
if not os.path.isdir(itempath): | |
continue # module must be a folder | |
if not os.path.isfile(os.path.join(itempath, '__init__.py')): | |
continue # module must be importable | |
if item not in settings.INSTALLED_APPS: | |
continue # module must be active in Django conf | |
yield os.path.join(path, item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You would then add it to you Django settings, for instance:
TEST_RUNNER='myproject.core.runners.TestRunner'