Test runner that installs 'tests' packages as apps

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from optparse import make_option

from django.conf import settings
from django.db.models.loading import load_app
from django.utils.importlib import import_module


try:
    # Django 1.6
    from django.test.runner import DiscoverRunner
except ImportError:
    # Fallback to third-party app on Django 1.5
    from discover_runner import DiscoverRunner


TESTAPPS_INSTALL = list(getattr(settings, 'TESTAPPS_INSTALL', () ))


class TestAppRunnerMixin(object):

    """
    A Django test runner mixin that, before testing and prior to 'syncdb', installs
    the 'tests' package from each of the apps specified.
    """

    option_list = (
        make_option(
            '--with-test-app',
            action='append',
            choices=settings.INSTALLED_APPS,
            dest='which_apps',
            default=settings.TESTAPPS_INSTALL,
            help="Specify a subset of INSTALLED_APPS whose 'tests' modules MUST be installed"
                 " as apps during test execution."
        ),
    )

    def __init__(self, which_apps, **options):
        self.which_apps = set(which_apps)  # remove duplicates
        super(TestAppRunnerMixin, self).__init__(**options)

    def setup_test_environment(self, **kwargs):
        self.install_test_apps()
        super(TestAppRunnerMixin, self).setup_test_environment(**kwargs)

    def install_test_apps(self):
        test_apps = []
        for app in self.which_apps:
            try:
                test_app = import_module('.tests', app)
                # check if it has a 'models' module and, if so, add it to the 'AppCache':
                if load_app(test_app.__name__):
                    test_apps.append(test_app.__name__)
            except ImportError:
                pass  # app with no 'tests' module
        settings.INSTALLED_APPS += tuple(test_apps)


class DiscoverTestAppRunner(TestAppRunnerMixin, DiscoverRunner):
    option_list = DiscoverRunner.option_list + TestAppRunnerMixin.option_list

More like this

  1. Deep package test runner by eternicode 3 years, 1 month ago
  2. PositionField by jpwatts 5 years, 9 months ago
  3. Twin column model admin index by richardbolt 6 years ago
  4. Run a testcase with custom INSTALLED_APPS by vdboor 1 year, 5 months ago
  5. Little middleware that create a facebook user by marinho 6 years, 4 months ago

Comments

(Forgotten your password?)