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

后端 未结 5 1631
小鲜肉
小鲜肉 2021-02-02 15:38

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
│   └         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 16:08

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

提交回复
热议问题