Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run tests for all my Django apps only?

Tags:

django

Right now, if I want to run tests from all my apps, I go:

python manage.py test app1 app2 app3

If I run:

python manage.py test

The test of all apps in INSTALLED_APPS are run, including the django ones. Is there a simple command to run the tests of all the apps that I have created?

like image 960
Thierry Lam Avatar asked Feb 24 '10 19:02

Thierry Lam


People also ask

How do I test my Django app?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.

How do I run a specific test case in Django?

If you want to run a test case class which has the path <module_name>/tests/test_views.py , you can run the command python manage.py test <module_name>. tests. test_views.

How do I run a specific app in Django?

It is possible to run a single app's tests standalone, without creating a Django test project for that purpose. One way of doing so is by creating a runtests.py in your app's root dir which setups Django settings and runs ./manage.py test your_app programmatically.

What order Django tests are run?

Order in which tests are executedAll TestCase subclasses are run first. Then, all other Django-based tests (test cases based on SimpleTestCase , including TransactionTestCase ) are run with no particular ordering guaranteed nor enforced among them.


3 Answers

Sadly there is no such command. Django has no way of telling which apps are "yours" versus which are someone else's.

What I would suggest is writing a new management command, call it mytest. Then create a new setting MY_INSTALLED_APPS. The mytest command will just run the test for every app in MY_INSTALLED_APPS. You'll want the mytest command to subclass django.core.management.base.AppCommand. django.core.management.call_command will also be helpful.

The only problem with this method is that you will have to constantly maintain the MY_INSTALLED_APPS setting to make sure it is correct.

like image 188
Apreche Avatar answered Oct 31 '22 20:10

Apreche


You could create an management/commands/testmyapps.py for one of your app which has:

from django.core.management.base import BaseCommand, CommandError
import django.core.management.commands.test
from django.core import management
from django.conf import settings

class Command(django.core.management.commands.test.Command):
    args = ''
    help = 'Test all of MY_INSTALLED_APPS'

    def handle(self, *args, **options):
        super(Command, self).handle(*(settings.MY_INSTALLED_APPS + args), **options)
like image 29
Miao ZhiCheng Avatar answered Oct 31 '22 18:10

Miao ZhiCheng


This works better in Django 1.6+: when you run python manage.py test, only your tests will run (assuming you have the default settings for TEST_RUNNER)

like image 39
Nils Avatar answered Oct 31 '22 20:10

Nils