src/ztfy/utils/tests/test_utilsdocstrings.py
branchZTK-1.1
changeset 148 d3668ecd9137
parent 70 82d8de021806
equal deleted inserted replaced
147:044dc196ec8a 148:d3668ecd9137
       
     1 ### -*- coding: utf-8 -*- ####################################################
       
     2 ##############################################################################
       
     3 #
       
     4 # Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
       
     5 # All Rights Reserved.
       
     6 #
       
     7 # This software is subject to the provisions of the Zope Public License,
       
     8 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
    10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    12 # FOR A PARTICULAR PURPOSE.
       
    13 #
       
    14 ##############################################################################
       
    15 
       
    16 """
       
    17 Generic Test case for ztfy.utils doc strings
       
    18 """
       
    19 __docformat__ = 'restructuredtext'
       
    20 
       
    21 import unittest
       
    22 import doctest
       
    23 import sys
       
    24 import os
       
    25 
       
    26 
       
    27 current_dir = os.path.abspath(os.path.dirname(__file__))
       
    28 
       
    29 def doc_suite(test_dir, globs=None):
       
    30     """Returns a test suite, based on doc tests strings found in /*.py"""
       
    31     suite = []
       
    32     if globs is None:
       
    33         globs = globals()
       
    34 
       
    35     flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE |
       
    36              doctest.REPORT_ONLY_FIRST_FAILURE)
       
    37 
       
    38     package_dir = os.path.split(test_dir)[0]
       
    39     if package_dir not in sys.path:
       
    40         sys.path.append(package_dir)
       
    41 
       
    42     # filtering files on extension
       
    43     docs = [doc for doc in
       
    44             os.listdir(package_dir) if doc.endswith('.py')]
       
    45     docs = [doc for doc in docs if not doc.startswith('__')]
       
    46 
       
    47     for test in docs:
       
    48         fd = open(os.path.join(package_dir, test))
       
    49         content = fd.read()
       
    50         fd.close()
       
    51         if '>>> ' not in content:
       
    52             continue
       
    53         test = test.replace('.py', '')
       
    54         location = 'ztfy.utils.%s' % test
       
    55         suite.append(doctest.DocTestSuite(location, optionflags=flags,
       
    56                                           globs=globs))
       
    57 
       
    58     return unittest.TestSuite(suite)
       
    59 
       
    60 def test_suite():
       
    61     """returns the test suite"""
       
    62     return doc_suite(current_dir)
       
    63 
       
    64 if __name__ == '__main__':
       
    65     unittest.main(defaultTest='test_suite')
       
    66