2

我的 Django 测试套件有一个小问题。

我正在开发一个可以在 Django 和 Plone (http://pypi.python.org/pypi/jquery.pyproxy) 中运行的 Python 包。所有测试都以 doctest 的形式编写,无论是在 Python 代码中还是在单独的 docfile(例如 README.txt)中。

我可以让这些测试运行良好,但 Django 只是不计算它们:

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

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

OK

但如果我有一些失败的测试,它会正确显示:

[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

这就是我的测试套件现在声明的方式:

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),

    }

我想我在声明测试套件时做错了,但我不知道它到底是什么。

谢谢你的帮助,文森特

4

1 回答 1

1

我终于用方法解决了这个问题suite()

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()])

显然,调用doctest.testfiledoctest.testmod直接运行测试时的问题。使用DocTestSuite/DocFileSuite构建列表,然后测试运行程序运行它们。

于 2011-10-03T08:16:19.017 回答