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