|
1 # -*- coding: utf-8 -*- |
|
2 # Copyright (c) 2008 Thierry Florac |
|
3 |
|
4 # This program is free software; you can redistribute it and/or modify |
|
5 # it under the terms of the GNU General Public License as published by |
|
6 # the Free Software Foundation; either version 2 of the License, or |
|
7 # (at your option) any later version. |
|
8 |
|
9 # This program is distributed in the hope that it will be useful, |
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 # GNU General Public License for more details. |
|
13 |
|
14 # You should have received a copy of the GNU General Public License |
|
15 # along with this program; see the file COPYING. If not, write to the |
|
16 # Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
17 """ |
|
18 Generic Test case for ztfy.utils doc strings |
|
19 """ |
|
20 __docformat__ = 'restructuredtext' |
|
21 |
|
22 import unittest |
|
23 import doctest |
|
24 import sys |
|
25 import os |
|
26 |
|
27 from zope.testing import doctest |
|
28 |
|
29 current_dir = os.path.abspath(os.path.dirname(__file__)) |
|
30 |
|
31 def doc_suite(test_dir, globs=None): |
|
32 """Returns a test suite, based on doc tests strings found in /*.py""" |
|
33 suite = [] |
|
34 if globs is None: |
|
35 globs = globals() |
|
36 |
|
37 flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | |
|
38 doctest.REPORT_ONLY_FIRST_FAILURE) |
|
39 |
|
40 package_dir = os.path.split(test_dir)[0] |
|
41 if package_dir not in sys.path: |
|
42 sys.path.append(package_dir) |
|
43 |
|
44 # filtering files on extension |
|
45 docs = [doc for doc in |
|
46 os.listdir(package_dir) if doc.endswith('.py')] |
|
47 docs = [doc for doc in docs if not doc.startswith('__')] |
|
48 |
|
49 for test in docs: |
|
50 fd = open(os.path.join(package_dir, test)) |
|
51 content = fd.read() |
|
52 fd.close() |
|
53 if '>>> ' not in content: |
|
54 continue |
|
55 test = test.replace('.py', '') |
|
56 location = 'ztfy.utils.%s' % test |
|
57 suite.append(doctest.DocTestSuite(location, optionflags=flags, |
|
58 globs=globs)) |
|
59 |
|
60 return unittest.TestSuite(suite) |
|
61 |
|
62 def test_suite(): |
|
63 """returns the test suite""" |
|
64 return doc_suite(current_dir) |
|
65 |
|
66 if __name__ == '__main__': |
|
67 unittest.main(defaultTest='test_suite') |
|
68 |