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

后端 未结 5 1635
小鲜肉
小鲜肉 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:04

    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
    

提交回复
热议问题