如何防止 Nose 运行和报告重复的测试?

发布于 2024-09-16 14:35:15 字数 666 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演多会厌 2024-09-23 14:35:15

解决此问题的任何方法(其他
比从不使用子模块)

不要包含

from test_views import *
from test_models import *

tests/__init__.py 中的行。这些线路有什么作用?

Any ways around this problem (other
than never using sub-modules)

Don't include the lines

from test_views import *
from test_models import *

in tests/__init__.py. What are those lines accomplishing?

孤单情人 2024-09-23 14:35:15

您可以有条件地进行导入。

假设您正在设置 TEST_RUNNER = 'django_nose.NoseTestSuiteRunner',以下代码可以解决问题:

from django.conf import settings
if 'nose' not in getattr(settings, 'TEST_RUNNER', ''):
    # Support Django test discovery
    from .test_views import *
    from .test_models import *

通过这种方式,您将能够支持正常的 Django 测试发现和鼻子测试发现,而无需重复测试后者,或因前者而失去它们。

You can do the import conditionally.

The following does the trick, assuming you are setting TEST_RUNNER = 'django_nose.NoseTestSuiteRunner':

from django.conf import settings
if 'nose' not in getattr(settings, 'TEST_RUNNER', ''):
    # Support Django test discovery
    from .test_views import *
    from .test_models import *

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文