src/pyams_template/tests/test_utilsdocs.py
changeset 17 e94c6b8d5b50
parent 0 31ded33115d7
equal deleted inserted replaced
16:ac179d1bfc0b 17:e94c6b8d5b50
     1 ### -*- coding: utf-8 -*- ####################################################
       
     2 ##############################################################################
       
     3 #
     1 #
     4 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
     5 # All Rights Reserved.
     3 # All Rights Reserved.
     6 #
     4 #
     7 # This software is subject to the provisions of the Zope Public License,
     5 # This software is subject to the provisions of the Zope Public License,
     9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
    10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    12 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    13 #
    11 #
    14 ##############################################################################
       
    15 
    12 
    16 """
    13 """
    17 Generic Test case for pyams_template doctest
    14 Generic Test case for pyams_template doctest
    18 """
    15 """
       
    16 
       
    17 import doctest
       
    18 import os
       
    19 import unittest
       
    20 
       
    21 from pyams_template.tests import get_package_dir
       
    22 
       
    23 
    19 __docformat__ = 'restructuredtext'
    24 __docformat__ = 'restructuredtext'
    20 
    25 
    21 import unittest
    26 CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
    22 import doctest
       
    23 import sys
       
    24 import os
       
    25 
    27 
    26 
    28 
    27 current_dir = os.path.dirname(__file__)
    29 def doc_suite(test_dir, setUp=None, tearDown=None, globs=None):  # pylint: disable=invalid-name
    28 
       
    29 def doc_suite(test_dir, setUp=None, tearDown=None, globs=None):
       
    30     """Returns a test suite, based on doctests found in /doctest."""
    30     """Returns a test suite, based on doctests found in /doctest."""
    31     suite = []
    31     suite = []
    32     if globs is None:
    32     if globs is None:
    33         globs = globals()
    33         globs = globals()
    34 
    34 
    35     flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE |
    35     flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE |
    36              doctest.REPORT_ONLY_FIRST_FAILURE)
    36              doctest.REPORT_ONLY_FIRST_FAILURE)
    37 
    37 
    38     package_dir = os.path.split(test_dir)[0]
    38     package_dir = get_package_dir(test_dir)
    39     if package_dir not in sys.path:
       
    40         sys.path.append(package_dir)
       
    41 
       
    42     doctest_dir = os.path.join(package_dir, 'doctests')
    39     doctest_dir = os.path.join(package_dir, 'doctests')
    43 
    40 
    44     # filtering files on extension
    41     # filtering files on extension
    45     docs = [os.path.join(doctest_dir, doc) for doc in
    42     docs = [os.path.join(doctest_dir, doc) for doc in
    46             os.listdir(doctest_dir) if doc.endswith('.txt')]
    43             os.listdir(doctest_dir) if doc.endswith('.txt') or doc.endswith('.rst')]
    47 
    44 
    48     for test in docs:
    45     for test in docs:
    49         suite.append(doctest.DocFileSuite(test, optionflags=flags,
    46         suite.append(doctest.DocFileSuite(test, optionflags=flags,
    50                                           globs=globs, setUp=setUp,
    47                                           globs=globs, setUp=setUp,
    51                                           tearDown=tearDown,
    48                                           tearDown=tearDown,
    52                                           module_relative=False))
    49                                           module_relative=False))
    53 
    50 
    54     return unittest.TestSuite(suite)
    51     return unittest.TestSuite(suite)
    55 
    52 
       
    53 
    56 def test_suite():
    54 def test_suite():
    57     """returns the test suite"""
    55     """returns the test suite"""
    58     return doc_suite(current_dir)
    56     return doc_suite(CURRENT_DIR)
       
    57 
    59 
    58 
    60 if __name__ == '__main__':
    59 if __name__ == '__main__':
    61     unittest.main(defaultTest='test_suite')
    60     unittest.main(defaultTest='test_suite')
    62