2011-09-29 43 views
2

我在使用Django測試套件時遇到了一個小問題。Django和docfiles中的測試

我正在研究可以在Django和Plone(http://pypi.python.org/pypi/jquery.pyproxy)中運行的Python包。 所有的測試都是作爲doctests編寫的,無論是在Python代碼中還是在單獨的docfiles中(例如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), 

    } 

我想我做錯了什麼時候聲明測試su但我不知道它到底是什麼。調用doctest.testfile或doctest.testmod的是,測試時

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

顯然的問題:

感謝您的幫助, 文森特

+1

不要把「我終於解決了......」放在問題中。刪除這個。創建一個答案。如果您解決了這個問題,您必須發佈您的解決方案作爲答案。將答案發布爲答案而不是延續問題要好得多。 –

+0

這就是我第一次嘗試,但直到幾個小時纔回答我自己的問題。我會盡可能更新。 – Vincent

回答

1

我的suite()方法終於解決了這個問題直接跑了。 使用DocTestSuite/DocFileSuite構建列表,然後測試運行器運行它們。