简体   繁体   English

我怎么告诉django-nose我的测试在哪里?

[英]How do I tell django-nose where my tests are?

I have my tests for a Django application in a tests directory: 我在测试目录中测试了Django应用程序:

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. Django测试运行器要求我在我的应用程序的tests目录的__init__.py文件中放入一个suite()函数。 That function returns the test cases that will run when I do $ python manage.py test 该函数返回当我执行$ python manage.py test时将运行的测试用例

I installed django-nose. 我安装了django-nose。 When I try to run the tests with django-nose, 0 tests are run: 当我尝试使用django-nose运行测试时,运行0测试:

$ 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? 为什么django-nose的测试跑者没有找到我的测试? What must I do? 我必须做什么?

If you use django-nose you can use a simple selector: 如果你使用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)

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 要在Django项目中使用它,只需在settigs.py中提供以下选项

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

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

nose will pick automatically, if you add test = True in the beginning of module and if your methods starts with test_ 如果你在模块的开头添加test = True并且你的方法以test_开头,那么nose会自动选择

see how nose detects tests http://nose.readthedocs.io/en/latest/finding_tests.html 看看鼻子如何检测测试http://nose.readthedocs.io/en/latest/finding_tests.html

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. 它非常hacky,但是我为解决这个问题所做的不是在__init__.py中定义一个套件,而是从所有其他测试模块中import * ,它很糟糕但是它有效。

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. 我做了一些事情来帮助确保我不会在其他模块中破坏测试套件...在每个测试模块中声明了__all__ ,因此只有测试名称导入了*并且保持pylint运行我我收到了类名重新定义的通知。

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: 说过你应该能够让这个工作没有任何丑陋的import *废话...我不使用鼻子和django-nose ...(我打算很快纠正)...因为这就是你看起来你可以这样做来运行你的apps目录中的所有测试:

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. 注意我没有在前面的例子中包含该项目...这似乎对我使用nosetests和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

To make nose realize where your apps are add these to the top of manage.py 要弄清楚应用程序的位置,请将这些内容添加到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 python manage.py test my_app.tests.storage_tests:TestCase.test_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM