Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing specific apps in Django

As far as I know, it is possible to run tests for either all installed apps or a single app. Testing all apps seem to be as an overkill as it usually contains also Django's native modules (django.contrib.auth etc). Is it possible to create a test suite containing only apps I specify (which would cointain all tests from all apps in my project folder in my case)?

like image 630
Ondrej Slinták Avatar asked Feb 22 '23 08:02

Ondrej Slinták


2 Answers

you could write a custom test runner that only tests your apps. in the past i've used something like

tests/runner.py:

from django.test.simple import DjangoTestSuiteRunner
from django.conf import settings

class AppsTestSuiteRunner(DjangoTestSuiteRunner):
    """ Override the default django 'test' command, include only
        apps that are part of this project
        (unless the apps are specified explicitly)
    """


    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        if not test_labels:
            PROJECT_PREFIX = 'my_project.'
            test_labels = [app.replace(PROJECT_PREFIX, '')
                for app in settings.INSTALLED_APPS
                if app.startswith(PROJECT_PREFIX)]
        return super(AppsTestSuiteRunner, self).run_tests(
              test_labels, extra_tests, **kwargs)

you then set this as your default test runner in settings.py

TEST_RUNNER = 'my_project.tests.runner.AppsTestSuiteRunner'
like image 57
second Avatar answered Mar 04 '23 01:03

second


Looks like you'd have to run the tests without the Django test runner.

https://docs.djangoproject.com/en/dev/topics/testing/#running-tests-outside-the-test-runner

like image 42
Acorn Avatar answered Mar 04 '23 01:03

Acorn