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