3

如何注册仅存在于单元测试中的自定义 Django 管理命令?

在 Django 1.5 中,您可以这样做:

from django.test import TestCase
from django.core.management import _commands

from myapp.testse.commands import MyTestCommand

class JobTestCase(TestCase):

    fixtures = ['test_jobs.json']

    def setUp(self):
        # Install the test command; this little trick installs the command
        # so that we can refer to it by name
        _commands['test_command'] = MyTestCommand()

但是,在 Django 1.8 中,_commands不再存在。查看代码,似乎这些现在是通过 django.core.management.get_commands 动态检索的,但我没有看到添加或注册任意命令类的方法。这可能吗?

4

0 回答 0