Python 将目录添加到Django测试的搜索路径
我使用一个django用于各种定制发行版。 我找到了一个解决方案,每个发行版特有的应用程序位于django项目文件夹之外(我们称之为Python 将目录添加到Django测试的搜索路径,python,django,unit-testing,Python,Django,Unit Testing,我使用一个django用于各种定制发行版。 我找到了一个解决方案,每个发行版特有的应用程序位于django项目文件夹之外(我们称之为DIR/),并使用 (添加到sys.path解决方案) 项目正在运行,但是现在它没有发现测试。在将应用程序移出Django项目文件夹之前,我可以简单地使用以下工具运行所有测试: manage.py test 但是,现在没有找到测试。除了将DIR/添加到settings.py之外,我还尝试将其添加到manage.py,但没有任何帮助。通过阅读我发现的django文档
DIR/
),并使用
(添加到sys.path解决方案)
项目正在运行,但是现在它没有发现测试。在将应用程序移出Django项目文件夹之前,我可以简单地使用以下工具运行所有测试:
manage.py test
但是,现在没有找到测试。除了将DIR/
添加到settings.py之外,我还尝试将其添加到manage.py,但没有任何帮助。通过阅读我发现的django文档,我可以指定如下模块:
manage.py test app_name.tests
上述方法可行,但对于许多应用程序来说是不切实际的。如何添加搜索测试的路径
我读过这篇文章,但它只描述了问题,而不是解决方案:
对我的项目结构的请求:
somefolder/
|-- dist/
| |-- dist1/apps/
| | |---- app11/
| | '---- app12/
| |
| '-- dist2/apps/
| |---- app21/
| '---- app22/
|-- src/
|-- manage.py
|-- project/settings.py
|-- appA/
'-- appB/
.
├── 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
自询问后,我们发现这是可行的(不理想):
关键是在您的
dist
、dist/dist1
和dist/dist1/apps
目录中添加一些空的\uuuu init\uuuu py
文件
我刚刚试着用以下结构组合一个玩具项目:
somefolder/
|-- dist/
| |-- dist1/apps/
| | |---- app11/
| | '---- app12/
| |
| '-- dist2/apps/
| |---- app21/
| '---- app22/
|-- src/
|-- manage.py
|-- project/settings.py
|-- appA/
'-- appB/
.
├── 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
请注意,在dist/
下的每个目录中都有一个空的\uuuu init\uuuuuuuuuu.py
文件。这使每个目录都成为一个目录,并使Python测试机器在这些目录中查找测试
还请注意,我有一个名为app\u internal
的内部应用程序和一个名为app\u external
的外部应用程序,它们位于与您类似的目录结构中
app_internal
和app_external
在tests.py
文件中都有一个伪测试
app\u external/tests.py
内容:
from django.test import TestCase
class ExternalTestCase(TestCase):
def test_fake(self):
self.assertTrue(False)
from django.test import TestCase
class InternalTestCase(TestCase):
def test_fake(self):
self.assertTrue(True)
app\u internal/tests.py
内容:
from django.test import TestCase
class ExternalTestCase(TestCase):
def test_fake(self):
self.assertTrue(False)
from django.test import TestCase
class InternalTestCase(TestCase):
def test_fake(self):
self.assertTrue(True)
在这里,我们预计app_外部
测试将失败,app_内部
测试将成功
我可以通过发出常规命令调用app\u 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\u 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'...
$ ./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'...
注意我是如何将。
作为参数传递给test命令的
这指向与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 1 test in 0.000s
FAILED (failures=1)
Destroying test database for alias 'default'...
$ ./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对此进行了测试。您的项目结构如何?你能分享一下吗?谢谢你的调查。我测试了您的方法,虽然它导入了它们,但问题是由于模块名称空间冲突,我没有init.py文件,这就是为什么在运行时将发行版分离并添加到syspath的原因。这并没有解释如何设置额外的搜索目录,只是解释如何使其搜索所有目录。如果没有其他人想出更好的办法,我会给你答案。这个答案提高了人们对导入过程的理解。谢谢