我在各种定制发行版中使用一个django。我找到了一个解决方案,每个发行版特有的应用程序位于django项目文件夹之外(让我们称之为DIR/),并使用如何将所有django应用程序保存在特定文件夹中 (添加到sys.path解决方案)将这个外部DIR/添加到Django路径中。
该项目正在运行,但是现在它没有发现测试。在将应用程序移出Django项目文件夹之前,我只需使用以下方法运行所有测试:
manage.py test然而,现在却找不到测试。除了将DIR/添加到settings.py之外,我还尝试将它添加到manage.py中,但没有帮助。通过读取django文档,我发现可以指定如下模块:
manage.py test app_name.tests上面的工作,但是不切实际的许多应用程序。如何添加在何处搜索测试的路径?
我读过这篇文章,但它只描述了问题,而不是解决方案:Django测试跑步者找不到测试
对我的项目结构的请求:
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发布于 2019-08-06 14:04:11
关键是在您的dist、dist/dist1和dist/dist1/apps目录中添加一些空的dist/dist1文件。
我刚刚尝试用以下结构组合了一个玩具项目:
.
├── 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包的原因,并使Python机器能够在这些目录中查找测试。
还请注意,在类似于您的目录结构中,我有一个名为app_internal的内部应用程序和一个名为app_external的外部应用程序。
app_internal和app_external在tests.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'...注意我是如何将..作为参数传递给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 2 tests in 0.001s
FAILED (failures=1)
Destroying test database for alias 'default'...我使用Django 1.11.23和Python2.7.12对此进行了测试。
https://stackoverflow.com/questions/57008770
复制相似问题