12 |
12 |
13 """ |
13 """ |
14 Generic Test case for pyams_pagelet doctest |
14 Generic Test case for pyams_pagelet doctest |
15 """ |
15 """ |
16 |
16 |
|
17 import doctest |
|
18 import os |
|
19 import unittest |
|
20 |
|
21 from pyams_pagelet.tests import get_package_dir |
|
22 |
|
23 |
17 __docformat__ = 'restructuredtext' |
24 __docformat__ = 'restructuredtext' |
18 |
25 |
19 import unittest |
26 CURRENT_DIR = os.path.abspath(os.path.dirname(__file__)) |
20 import doctest |
|
21 import sys |
|
22 import os |
|
23 |
27 |
24 |
28 |
25 current_dir = os.path.dirname(__file__) |
29 def doc_suite(test_dir, setUp=None, tearDown=None, globs=None): # pylint: disable=invalid-name |
26 |
30 """Returns a test suite, based on doctests found in /doctests""" |
27 def doc_suite(test_dir, setUp=None, tearDown=None, globs=None): |
|
28 """Returns a test suite, based on doctests found in /doctest.""" |
|
29 suite = [] |
31 suite = [] |
30 if globs is None: |
32 if globs is None: |
31 globs = globals() |
33 globs = globals() |
32 |
34 |
33 flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | |
35 flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | |
34 doctest.REPORT_ONLY_FIRST_FAILURE) |
36 doctest.REPORT_ONLY_FIRST_FAILURE) |
35 |
37 |
36 package_dir = os.path.split(test_dir)[0] |
38 package_dir = get_package_dir(test_dir) |
37 if package_dir not in sys.path: |
|
38 sys.path.append(package_dir) |
|
39 |
|
40 doctest_dir = os.path.join(package_dir, 'doctests') |
39 doctest_dir = os.path.join(package_dir, 'doctests') |
41 |
40 |
42 # filtering files on extension |
41 # filtering files on extension |
43 docs = [os.path.join(doctest_dir, doc) for doc in |
42 docs = [os.path.join(doctest_dir, doc) for doc in |
44 os.listdir(doctest_dir) if doc.endswith('.txt')] |
43 os.listdir(doctest_dir) if doc.endswith('.txt') or doc.endswith('.rst')] |
45 |
44 |
46 for test in docs: |
45 for test in docs: |
47 suite.append(doctest.DocFileSuite(test, optionflags=flags, |
46 suite.append(doctest.DocFileSuite(test, optionflags=flags, |
48 globs=globs, setUp=setUp, |
47 globs=globs, setUp=setUp, |
49 tearDown=tearDown, |
48 tearDown=tearDown, |
50 module_relative=False)) |
49 module_relative=False)) |
51 |
50 |
52 return unittest.TestSuite(suite) |
51 return unittest.TestSuite(suite) |
53 |
52 |
|
53 |
54 def test_suite(): |
54 def test_suite(): |
55 """returns the test suite""" |
55 """returns the test suite""" |
56 return doc_suite(current_dir) |
56 return doc_suite(CURRENT_DIR) |
|
57 |
57 |
58 |
58 if __name__ == '__main__': |
59 if __name__ == '__main__': |
59 unittest.main(defaultTest='test_suite') |
60 unittest.main(defaultTest='test_suite') |
60 |
|