سؤال

I'm having a small problem with my test suite with Django.

I'm working on a Python package that can run in both Django and Plone (http://pypi.python.org/pypi/jquery.pyproxy). All the tests are written as doctests, either in the Python code or in separate docfiles (for example the README.txt).

I can have those tests running fine but Django just do not count them:

[vincent ~/buildouts/tests/django_pyproxy]> bin/django test pyproxy
...
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

But if I had some failing test, it will appear correctly:

[vincent ~/buildouts/tests/django_pyproxy]> bin/django test pyproxy
...
Failed example:
    1+1
Expected nothing
Got:
    2
**********************************************************************
1 items had failures:
   1 of  44 in README.rst
***Test Failed*** 1 failures.
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

This is how my test suite is declared right now:

import os
import doctest
from unittest import TestSuite

from jquery.pyproxy import base, utils

OPTIONFLAGS = (doctest.ELLIPSIS |
           doctest.NORMALIZE_WHITESPACE)

__test__ = {
    'base': doctest.testmod(
        m=base,
        optionflags=OPTIONFLAGS),

    'utils': doctest.testmod(
        m=utils,
        optionflags=OPTIONFLAGS),

    'readme': doctest.testfile(
        "../../../README.rst",
        optionflags=OPTIONFLAGS),

    'django': doctest.testfile(
        "django.txt",
        optionflags=OPTIONFLAGS),

    }

I guess I'm doing something wrong when declaring the test suite but I don't have a clue what it is exactly.

Thanks for your help, Vincent

هل كانت مفيدة؟

المحلول

I finally solved the problem with the suite() method:

import os
import doctest
from django.utils import unittest

from jquery.pyproxy import base, utils

OPTIONFLAGS = (doctest.ELLIPSIS |
               doctest.NORMALIZE_WHITESPACE)

testmods = {'base': base,
            'utils': utils}
testfiles = {'readme': '../../../README.rst',
             'django': 'django.txt'}

def suite():
    return unittest.TestSuite(
        [doctest.DocTestSuite(mod, optionflags = OPTIONFLAGS)
         for mod in testmods.values()] + \
        [doctest.DocFileSuite(f, optionflags = OPTIONFLAGS)
         for f in testfiles.values()])

Apparently the problem when calling doctest.testfile or doctest.testmod is that the tests are directly ran. Using DocTestSuite/DocFileSuite builds the list and then the test runner runs them.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top