我如何告诉 django-nose 我的测试在哪里?

发布于 2024-11-08 17:17:28 字数 618 浏览 6 评论 0原文

我在测试目录中对 Django 应用程序进行了测试:

my_project/apps/my_app/
├── __init__.py
├── tests
│   ├── __init__.py
│   ├── field_tests.py
│   └── storage_tests.py
├── urls.py
├── utils.py
└── views.py

Django 测试运行程序要求我将 suite() 函数放入应用程序测试目录的 __init__.py 文件中。该函数返回我执行时将运行的测试用例 $ python manage.py test

我安装了 django-nose。当我尝试使用 django-nose 运行测试时,会运行 0 个测试:

$ python manage.py test <app_name>

如果我直接指向测试模块,则会运行测试:

$ python manage.py test my_project.apps.my_app.tests.storage_tests

为什么 django-nose 的测试运行程序找不到我的测试?我必须做什么?

I have my tests for a Django application in a tests directory:

my_project/apps/my_app/
├── __init__.py
├── tests
│   ├── __init__.py
│   ├── field_tests.py
│   └── storage_tests.py
├── urls.py
├── utils.py
└── views.py

The Django test runner requires that I put a suite() function in the __init__.py file of my application's tests directory. That function returns the test cases that will run when I do
$ python manage.py test

I installed django-nose. When I try to run the tests with django-nose, 0 tests are run:

$ python manage.py test <app_name>

If I point directly at the test module, the tests are run:

$ python manage.py test my_project.apps.my_app.tests.storage_tests

Why does django-nose's test runner not find my tests? What must I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

傲鸠 2024-11-15 17:17:28

如果您使用 django-nose,您可以使用一个简单的选择器:

from nose.selector import Selector
from nose.plugins import Plugin
import os
import django.test

class TestDiscoverySelector(Selector):
    def wantDirectory(self, dirname):
        parts = dirname.split(os.path.sep)
        return 'my_app' in parts

    def wantClass(self, cls):
        return issubclass(cls, django.test.TestCase)

    def wantFile(self, filename):
        parts = filename.split(os.path.sep)
        return 'test' in parts and filename.endswith('.py')

    def wantModule(self, module):
        parts = module.__name__.split('.')
        return 'test' in parts

class TestDiscoveryPlugin(Plugin):
    enabled = True

    def configure(self, options, conf):
        pass

    def prepareTestLoader(self, loader):
        loader.selector = TestDiscoverySelector(loader.config)

这只是一个示例实现,您可以使其更具可配置性或根据您的需要进行调整。要在 Django 项目中使用它,只需在 settigs.py 中提供以下选项

NOSE_PLUGINS = ['lorepo.utility.noseplugins.TestDiscoveryPlugin']

If you use django-nose you can use a simple selector:

from nose.selector import Selector
from nose.plugins import Plugin
import os
import django.test

class TestDiscoverySelector(Selector):
    def wantDirectory(self, dirname):
        parts = dirname.split(os.path.sep)
        return 'my_app' in parts

    def wantClass(self, cls):
        return issubclass(cls, django.test.TestCase)

    def wantFile(self, filename):
        parts = filename.split(os.path.sep)
        return 'test' in parts and filename.endswith('.py')

    def wantModule(self, module):
        parts = module.__name__.split('.')
        return 'test' in parts

class TestDiscoveryPlugin(Plugin):
    enabled = True

    def configure(self, options, conf):
        pass

    def prepareTestLoader(self, loader):
        loader.selector = TestDiscoverySelector(loader.config)

This is just an example implementation and you can make it more configurable or adjust it to your needs. To use it in your Django project just provide the following option in settigs.py

NOSE_PLUGINS = ['lorepo.utility.noseplugins.TestDiscoveryPlugin']
甜柠檬 2024-11-15 17:17:28

错误:

python manage.py test my_app.tests.storage_tests:TestCase.test_name

实际上是在类名之前加上斜杠并加上文件扩展名

正确:

python manage.py test my_app/tests/storage_tests.py:TestCase.test_name

Wrong:

python manage.py test my_app.tests.storage_tests:TestCase.test_name

is in fact with slashes until the class name and with the extension of the file

Right:

python manage.py test my_app/tests/storage_tests.py:TestCase.test_name
定格我的天空 2024-11-15 17:17:28

如果您在模块的开头添加 test = True 并且您的方法以 test_ 开头,nose 会自动选择,

请参阅nose 如何检测测试 http://nose.readthedocs.io/en/latest/finding_tests.html

nose will pick automatically, if you add test = True in the beginning of module and if your methods starts with test_

see how nose detects tests http://nose.readthedocs.io/en/latest/finding_tests.html

从﹋此江山别 2024-11-15 17:17:28

我也遇到过同样的情况。使用该套件,您可以运行所有测试或应用程序的测试,但不能运行特定的测试用例或特定的测试。

这是相当 hacky 的,但我为解决这个问题所做的不是在 __init__.py 中定义一个套件,而是从所有其他测试模块中导入 * ,这很糟糕,但它有效。

我做了一些事情来帮助确保我不会最终破坏其他模块中的测试套件...在每个测试模块中声明 __all__ ,以便仅导入测试名称* 并保持 pylint 运行 我收到类名重新定义的通知。

话虽如此,你应该能够让它工作,而不需要任何丑陋的 import * 废话...我不使用鼻子和 django-nose...(我打算很快纠正)。 ..因为这就是您正在做的事情,看起来您可以这样做来运行应用程序目录中的所有测试:

python manage.py test apps

或者运行单个测试模块的所有测试:

python manage.py test apps.my_app.tests.storage_tests

请注意,我没有将该项目包含在前面的例子......使用nosetests对我来说似乎工作得很好姜戈鼻子。

另外,要运行特定的测试套件,您可以执行以下操作:

python manage.py test apps.my_app.tests.storage_tests:TestCase

或者运行一个特定的测试:

python manage.py test apps.my_app.tests.storage_tests:TestCase.test_name

I have run into this same situation as well. Using the suite it will allow you to run all tests, or tests for an app, but not a specific test case or a specific test.

It's pretty hacky, but what I've done to get around this is instead of defining a suite in __init__.py is to just import * from all of the other test modules, it sucks but it works.

There are a few things that I do to help make sure that I don't end up clobbering test suites in other modules... have __all__ declared in each test modules so only the test names are imported with the * and keep pylint running I'm notified of class name redefinitions.

Having said that you should be able to get this to work without any ugly import * crap... I do not use nose and django-nose...(which I intend to correct very soon)... since that is what you're doing it looks like you can do this to run all of the tests in your apps directory:

python manage.py test apps

or to run all of the tests for a single test module:

python manage.py test apps.my_app.tests.storage_tests

notice I did not include the project in the previous example... which seemed to work fine for me using nosetests and django-nose.

Also, to run a specific test suite you can do this:

python manage.py test apps.my_app.tests.storage_tests:TestCase

Or to run one specific test:

python manage.py test apps.my_app.tests.storage_tests:TestCase.test_name
醉梦枕江山 2024-11-15 17:17:28

为了让鼻子意识到你的应用程序的位置,将它们添加到manage.py的顶部,

import os, site

ROOT = os.path.dirname(os.path.abspath(__file__))
path = lambda *a: os.path.join(ROOT, *a)
site.addsitedir(path('apps'))

从那时起你可以使用

python manage.py test my_app.tests.storage_tests:TestCase.test_name

To make nose realize where your apps are add these to the top of manage.py

import os, site

ROOT = os.path.dirname(os.path.abspath(__file__))
path = lambda *a: os.path.join(ROOT, *a)
site.addsitedir(path('apps'))

Since then you can use

python manage.py test my_app.tests.storage_tests:TestCase.test_name

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