我如何告诉django-nose我的测试是什么? [英] How do I tell django-nose where my tests are?

查看:145
本文介绍了我如何告诉django-nose我的测试是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在测试目录中有一个Django应用程序的测试:

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

Django测试运行程序要求我将一个suite()函数放在我的应用程序测试目录的 __ init __。py 文件中。该函数返回测试用例,当我执行
$ python manage.py test

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

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

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

为什么django-nose的测试运行器没有找到我的测试?我应该怎么做?

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

推荐答案

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

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)

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

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']

这篇关于我如何告诉django-nose我的测试是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆