我的 Django 测试套件有一个小问题。
我正在开发一个可以在 Django 和 Plone 中运行的 Python 包 (http://pypi.python.org/pypi/jquery.pyproxy)。 所有测试都以 doctests 的形式编写,要么在 Python 代码中,要么在单独的文档文件(例如 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),
}
我想我在声明测试套件时做错了什么,但我不知道到底是什么。
感谢您的帮助, 文森特
请您参考如下方法:
我终于用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.testfile 或 doctest.testmod 时的问题是测试是直接运行的。 使用 DocTestSuite/DocFileSuite 构建列表,然后测试运行器运行它们。
