ztfy/utils/tests/test_utilsdocs.py
changeset 0 712d20d2751e
child 48 d9a45c366c6c
equal deleted inserted replaced
-1:000000000000 0:712d20d2751e
       
     1 # -*- coding: utf-8 -*-
       
     2 # Copyright (c) 2008 Thierry Florac
       
     3 
       
     4 # This program is free software; you can redistribute it and/or modify
       
     5 # it under the terms of the GNU General Public License as published by
       
     6 # the Free Software Foundation; either version 2 of the License, or
       
     7 # (at your option) any later version.
       
     8 
       
     9 # This program is distributed in the hope that it will be useful,
       
    10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 # GNU General Public License for more details.
       
    13 
       
    14 # You should have received a copy of the GNU General Public License
       
    15 # along with this program; see the file COPYING. If not, write to the
       
    16 # Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
       
    17 """
       
    18 Generic Test case for ztfy.utils doctest
       
    19 """
       
    20 __docformat__ = 'restructuredtext'
       
    21 
       
    22 import unittest
       
    23 import doctest
       
    24 import sys
       
    25 import os
       
    26 
       
    27 from zope.testing import doctest
       
    28 
       
    29 current_dir = os.path.dirname(__file__)
       
    30 
       
    31 def doc_suite(test_dir, setUp=None, tearDown=None, globs=None):
       
    32     """Returns a test suite, based on doctests found in /doctest."""
       
    33     suite = []
       
    34     if globs is None:
       
    35         globs = globals()
       
    36 
       
    37     flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE |
       
    38              doctest.REPORT_ONLY_FIRST_FAILURE)
       
    39 
       
    40     package_dir = os.path.split(test_dir)[0]
       
    41     if package_dir not in sys.path:
       
    42         sys.path.append(package_dir)
       
    43 
       
    44     doctest_dir = os.path.join(package_dir, 'doctests')
       
    45 
       
    46     # filtering files on extension
       
    47     docs = [os.path.join(doctest_dir, doc) for doc in
       
    48             os.listdir(doctest_dir) if doc.endswith('.txt')]
       
    49 
       
    50     for test in docs:
       
    51         suite.append(doctest.DocFileSuite(test, optionflags=flags, 
       
    52                                           globs=globs, setUp=setUp, 
       
    53                                           tearDown=tearDown,
       
    54                                           module_relative=False))
       
    55 
       
    56     return unittest.TestSuite(suite)
       
    57 
       
    58 def test_suite():
       
    59     """returns the test suite"""
       
    60     return doc_suite(current_dir)
       
    61 
       
    62 if __name__ == '__main__':
       
    63     unittest.main(defaultTest='test_suite')
       
    64