django doctests not being run

2019-05-07 09:56发布

问题:

I'm having a problem running django doctests with django-nose. Unit tests added to a /tests directory are running fine, but doctests are not.

I am trying to run doctests on my "season" module:

python manage.py test season

and get this output:

nosetests --verbosity 1 season --with-doctest
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.001s

OK
Destroying test database for alias 'default'...

I'm just trying a basic doctest to try to get this to work, e.g.:

    """
    >>> 1+1 == 2
    True
    """

This is in my models.py. I've tried other doctests actually testing the code and still don't see any tests run. When I run with --verbosity 3, I see one line that may be the issue:

nose.selector: INFO: <models.py path> is executable; skipped

I couldn't find any more info on what this means though.

Relevant snippets from settings.py:

INSTALLED_APPS = (
    'south',
    'django_nose',
    'season',
)

# Django-nose configuration
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = ['--with-doctest']

django_nose is after south in INSTALLED_APPS as specified in the django-nose documentation. I'm using the --with-doctest argument as suggested here: Nose not running Django doctests, and have updated my django-nose to the latest as suggested here: Why isn't django-nose running the doctests in my models?

These are the versions I am using:
django 1.3
python 2.7.1
django-nose 0.1.3
nose 1.1.2

I feel like I'm missing some basic setup here. Let me know if any other info is needed. Any help is appreciated, thanks!

回答1:

The verbosity message is telling you your models.py file is being ignored because it's executable. This means you need to do:

chmod -x models.py

Unless you have a specific reason for that file to be set as executable, in which case adding --exe to your NOSE_ARGS should be sufficient.



回答2:

I realize that the OP specified 1.3, but since this answer comes up in a search for 'django doctests do not run' here's my answer for 1.6 from one of the answers in Django doctests in views.py. in this version of Django doctests are not automatically included, so in $APP/tests.py you need:

import doctest
def load_tests(loader, tests, ignore):
    tests.addTests(doctest.DocTestSuite())
    return tests

[this only finds the doctests in tests.py itself; to have it run doctests on other modules, say myapp/models.py, you need to from myapp import models and tests.addTests(doctest.DocTestSuite(models))]



回答3:

nosetests --verbosity 1 season --with-doctest

Usage: manage.py test [options] [appname ...]

Perhaps you just need to move season to then end.