Example #1
0
    def setup(self, verbosity, testLabels):
        # Force declaring available_apps in TransactionTestCase for faster
        # tests.
        def noAvailableApps(self):
            raise Exception('Please define available_apps in'
                            ' TransactionTestCase and its subclasses.')
        TransactionTestCase.available_apps = property(noAvailableApps)
        TestCase.available_apps = None

        state = self.prepareSettings(settings)

        if verbosity > 0:
            # Ensure any warnings captured to logging are piped through a
            # verbose logging handler. If any -W options were passed
            # explicitly  on command line, warnings are not captured, and
            # this has no effect.
            logger = logging.getLogger('py.warnings')
            handler = logging.StreamHandler()
            logger.addHandler(handler)

        # Load all the self.alwaysInstalledApps.
        django.setup()

        # Reduce given test labels to just the app module path
        appNames = set(l.split('.', 1)[0] for l in testLabels)

        # Load all the test model apps.
        if verbosity >= 2:
            self.stream.writeln('Importing applications ...')

        for name in appNames:
            if verbosity >= 2:
                self.stream.writeln('Importing application {0}'.format(name))

            module = importlib.import_module(name)
            config = AppConfig(name, module)
            config.label = '_'.join((config.label.strip('_'), 'tests'))
            settings.INSTALLED_APPS.append(config)

        apps.set_installed_apps(settings.INSTALLED_APPS)

        return state