将目录添加到Django搜索路径以进行测试

时间:2019-07-12 14:13:45

标签: python django unit-testing

我正在使用一个django进行各种自定义发行。 我提出了一个解决方案,其中每个发行版所独有的应用程序都位于django projects文件夹之外(称为DIR/),然后使用DIR/外部将其添加到Django路径中,使用 How to keep all my django applications in specific folder (添加到sys.path解决方案中)

该项目正在运行,但是现在它没有发现测试。在将应用程序移出Django项目文件夹之前,我可以使用以下命令运行所有测试:

manage.py test

但是,现在找不到测试。除了将DIR/添加到settings.py之外,我尝试将其添加到manage.py中也没有帮助。通过阅读我发现的django文档,我可以指定一个像这样的模块:

manage.py test app_name.tests

以上方法有效,但对于许多应用程序来说并不切合实际。如何添加在哪里搜索测试的路径?

我读了这篇,但它仅描述了问题,而不是解决方案: Django test runner not finding tests

请求我的项目结构:

somefolder/
  |-- dist/
  |     |-- dist1/apps/
  |     |       |---- app11/
  |     |       '---- app12/
  |     |
  |     '-- dist2/apps/
  |             |---- app21/
  |             '---- app22/
  |-- src/
        |-- manage.py
        |-- project/settings.py
        |-- appA/
        '-- appB/

自从询问以来发现了这种方法(不理想):

manage.py test app11 app12 app21 app22 --keepdb

1 个答案:

答案 0 :(得分:2)

关键是要在__init__pydistdist/dist1目录中添加一些空的dist/dist1/apps文件。

我只是尝试将一个具有以下结构的玩具项目组合在一起:

.
├── dist
│   ├── dist1
│   │   ├── apps
│   │   │   ├── app_external
│   │   │   │   ├── admin.py
│   │   │   │   ├── apps.py
│   │   │   │   ├── __init__.py
│   │   │   │   ├── migrations
│   │   │   │   │   └── __init__.py
│   │   │   │   ├── models.py
│   │   │   │   ├── tests.py
│   │   │   │   └── views.py
│   │   │   └── __init__.py
│   │   └── __init__.py
│   └── __init__.py
└── src
    ├── app_internal
    │   ├── admin.py
    │   ├── apps.py
    │   ├── __init__.py
    │   ├── migrations
    │   │   └── __init__.py
    │   ├── models.py
    │   ├── tests.py
    │   └── views.py
    ├── manage.py
    └── project
        ├── __init__.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

请注意,__init__.py下的每个目录中都有一个空的dist/文件。这就是使每个目录成为Python package并使Python测试机制在这些目录中查找测试的原因。

还请注意,在与您的目录结构类似的目录结构中,我有一个内部应用程序app_internal和一个外部应用程序app_external

app_internalapp_externaltests.py文件中都有一个假测试。

app_external/tests.py内容:

from django.test import TestCase

class ExternalTestCase(TestCase):

    def test_fake(self):
        self.assertTrue(False)

app_internal/tests.py内容:

from django.test import TestCase

class InternalTestCase(TestCase):

    def test_fake(self):
        self.assertTrue(True)

在这里,我们期望app_external测试将会失败,而app_internal测试将会成功。

我可以通过发出通常的命令来调用app_internal测试:

$ ./manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
Destroying test database for alias 'default'...

然后我可以通过发出以下命令来调用app_external测试:

$ ./manage.py test ..
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_fake (dist.dist1.apps.app_external.tests.ExternalTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/augusto/develop/apps_outside/dist/dist1/apps/app_external/tests.py", line 10, in test_fake
    self.assertTrue(False)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)
Destroying test database for alias 'default'...

请注意如何将..作为参数传递给测试命令。

这指向相对于src/的父目录,因此它将找到dist包及其中的所有其他包。我希望这会在您的dist/目录下找到所有测试。

我还可以通过以下命令一次运行所有内部和外部测试:

$ ./manage.py test . ..
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.F
======================================================================
FAIL: test_fake (dist.dist1.apps.app_external.tests.ExternalTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/augusto/develop/apps_outside/dist/dist1/apps/app_external/tests.py", line 10, in test_fake
    self.assertTrue(False)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)
Destroying test database for alias 'default'...

我使用Django 1.11.23和Python 2.7.12进行了测试。

相关问题