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