1 # Copyright (c) 2008 by Florian Friesdorf
3 # GNU Affero General Public License (AGPL)
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public
16 # License along with this program. If not, see
17 # <http://www.gnu.org/licenses/>.
20 __author__
= "Florian Friesdorf <flo@chaoflow.net>"
21 __docformat__
= "plaintext"
28 from zope
.component
import testing
30 from zope
.testing
import doctest
31 from zope
.testing
import doctestunit
33 from paula
.testing
.utils
import hasdoctests
34 from paula
.testing
.utils
import ispackagedir
35 from paula
.testing
.utils
import pkgpath
36 from paula
.testing
.utils
import recursedir
37 from paula
.testing
.utils
import saneimport
39 from paula
.testing
.globs
import test_globs
42 doctest
.REPORT_ONLY_FIRST_FAILURE
+ \
44 doctest
.NORMALIZE_WHITESPACE
48 """We can use this to set up anything that needs to be available for
49 each test. It is run before each test, i.e. for each docstring that
52 Look at the Python unittest and doctest module documentation to learn
53 more about how to prepare state and pass it into various tests.
56 for k
,v
in test_globs
.items():
62 testing
.tearDown(test
)
64 def scanfordoctest(file):
65 """Decides whether a file should be scanned for doctests
68 - all .txt files for which a .py file exists
70 # XXX: not reimplemented
71 # # skip if it starts with a dot
72 # if item.startswith('.'):
75 if file.endswith('.py'):
76 return hasdoctests(file)
78 if file.endswith('.txt'):
79 pyfile
= file.replace('.txt','.py')
80 if os
.path
.isfile(pyfile
):
81 return hasdoctests(file)
86 def get_test_suite(pkgname
, files
=[]):
87 """construct a test suite for a package
89 test suite will contain all doctests found somewhere in the package and
90 the tests passed as argument
92 1. get root folder for package
93 2. recurse and find everything that might contain a doctest
94 3. create the test suite
98 def testsuite(doctestfile
):
99 if doctestfile
.endswith('.txt'):
100 doctest
= doctestunit
.DocFileSuite(doctestfile
,
102 setUp
=setUp
, tearDown
=tearDown
,
103 optionflags
=optionflags
,
107 if doctestfile
.endswith('.py'):
108 module
= doctestfile
.replace('.py','').replace(os
.sep
, '.')
109 module
= '.'.join((pkgname
, module
,))
110 module
= saneimport(module
)
111 doctest
= doctestunit
.DocTestSuite(module
,
112 setUp
=setUp
, tearDown
=tearDown
,
113 optionflags
=optionflags
,
120 pkg
= saneimport(pkgname
)
123 doctestfiles
= recursedir(
126 filefilter
=scanfordoctest
,
128 # make relative to pkg path
129 doctestfiles
= [x
[len(path
)+1:] for x
in doctestfiles
]
130 doctests
= [testsuite(x
) for x
in doctestfiles
+files
]
132 test_suite
= unittest
.TestSuite(doctests
)