如何防止 Nose 运行和报告重复的测试?
我正在使用 django-nose 来测试我们的 Django 项目。在 Django 中的应用程序中分割大型测试套件是很常见的,如下所示:
myapp/
__init__.py
models.py
tests/
__init__.py
test_views.py
test_models.py
views.py
tests/__init__.py
看起来像这样:
from test_views import *
from test_models import *
因为 Django 将在 myapp.tests
中查找测试,一切都按预期进行。另一方面,Nose 会在 tests_*.py
中找到测试并在 __init__.py
中再次导入它们。这导致报告的测试总数是应有的两倍。
有什么方法可以解决这个问题(除了从不使用子模块),可以正确报告 django-nose 和默认 Django 测试运行器的测试?
I'm using django-nose to test our Django projects. It is common to split large test suites inside an application in Django like this:
myapp/
__init__.py
models.py
tests/
__init__.py
test_views.py
test_models.py
views.py
tests/__init__.py
would look like this:
from test_views import *
from test_models import *
Since Django will look for tests in myapp.tests
, everything works as expected. Nose on the other hand will find the tests in tests_*.py
and import them again in __init__.py
. This results in the total number of tests reported being double what they should be.
Any ways around this problem (other than never using sub-modules) that will correctly report the tests with both django-nose and the default Django test runner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要包含
tests/__init__.py
中的行。这些线路有什么作用?Don't include the lines
in
tests/__init__.py
. What are those lines accomplishing?您可以有条件地进行导入。
假设您正在设置
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
,以下代码可以解决问题:通过这种方式,您将能够支持正常的 Django 测试发现和鼻子测试发现,而无需重复测试后者,或因前者而失去它们。
You can do the import conditionally.
The following does the trick, assuming you are setting
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
:In this way, you will be able to support both the normal Django test discovery and nose test discovery without duplicating tests for the latter, or losing them for the former.