如何只对所有Django应用程序运行测试?

时间:2022-12-06 07:31:32

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?

在INSTALLED_APPS中运行所有应用程序的测试,包括django应用程序。是否有一个简单的命令来运行我创建的所有应用程序的测试?

4 个解决方案

#1


23  

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

可惜没有这样的命令。Django无法区分哪些应用是“您的”,哪些是其他人的。

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.

我的建议是编写一个新的管理命令,称为mytest。然后创建一个新的设置MY_INSTALLED_APPS。mytest命令将为MY_INSTALLED_APPS中的每个应用程序运行测试。您将希望mytest命令子类化django.core.management.base.AppCommand。django.core.management。call_command也会有帮助。

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.

这种方法的唯一问题是,您必须不断地维护MY_INSTALLED_APPS设置,以确保它是正确的。

#2


2  

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

您可以创建一个管理/命令/ testapps。py为你的一个应用,它有:

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)

#3


2  

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)

这在Django 1.6+中工作得更好:当您运行python管理时。py测试,只有您的测试才会运行(假设您有TEST_RUNNER的默认设置)

#4


0  

Use nose django test runner, it takes care of this.

用鼻子django测试跑步者,它负责这个。

Reference: Django nose to run only project tests

引用:Django nose只运行项目测试

#1


23  

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

可惜没有这样的命令。Django无法区分哪些应用是“您的”,哪些是其他人的。

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.

我的建议是编写一个新的管理命令,称为mytest。然后创建一个新的设置MY_INSTALLED_APPS。mytest命令将为MY_INSTALLED_APPS中的每个应用程序运行测试。您将希望mytest命令子类化django.core.management.base.AppCommand。django.core.management。call_command也会有帮助。

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.

这种方法的唯一问题是,您必须不断地维护MY_INSTALLED_APPS设置,以确保它是正确的。

#2


2  

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

您可以创建一个管理/命令/ testapps。py为你的一个应用,它有:

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)

#3


2  

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)

这在Django 1.6+中工作得更好:当您运行python管理时。py测试,只有您的测试才会运行(假设您有TEST_RUNNER的默认设置)

#4


0  

Use nose django test runner, it takes care of this.

用鼻子django测试跑步者,它负责这个。

Reference: Django nose to run only project tests

引用:Django nose只运行项目测试