experimental DISABLE_DOCTEST
[paula/paual.testing.git] / src / paula / testing / testing.py
blob1ee50b745a8ca089fe8893858c0607d9c84e2bba
1 # Copyright (c) 2008-2009 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/>.
19 __author__ = "Florian Friesdorf <flo@chaoflow.net>"
20 __docformat__ = "plaintext"
22 import os
23 import os.path
24 import types
25 import unittest
27 from zope.component import testing
29 from zope.testing import doctest
30 from zope.testing import doctestunit
32 from paula.testing.utils import hasdoctests
33 from paula.testing.utils import ispackagedir
34 from paula.testing.utils import pkgpath
35 from paula.testing.utils import recursedir
36 from paula.testing.utils import saneimport
38 from paula.testing.globs import test_globs
40 optionflags = \
41 doctest.REPORT_ONLY_FIRST_FAILURE + \
42 doctest.ELLIPSIS + \
43 doctest.NORMALIZE_WHITESPACE
46 def setUp(test):
47 """We can use this to set up anything that needs to be available for
48 each test. It is run before each test, i.e. for each docstring that
49 contains doctests.
51 Look at the Python unittest and doctest module documentation to learn
52 more about how to prepare state and pass it into various tests.
53 """
54 testing.setUp(test)
55 for k,v in test_globs.items():
56 test.globs[k] = v
57 test.globs['testinstance'] = test
59 def testfile(path):
60 if not path.startswith(os.sep):
61 path = os.sep.join(test.filename.split(os.sep)[:-1]+ [path,])
62 return file(path)
63 test.globs['testfile'] = testfile
65 def tearDown(test):
66 """
67 """
68 testing.tearDown(test)
70 def scanfordoctest(file):
71 """Decides whether a file should be scanned for doctests
73 - all .py files
74 - all .txt files for which a .py file exists
75 """
76 # XXX: not reimplemented
77 # # skip if it starts with a dot
78 # if item.startswith('.'):
79 # continue
81 if file.endswith('.py'):
82 return hasdoctests(file)
84 if file.endswith('.txt'):
85 pyfile = file.replace('.txt','.py')
86 if os.path.isfile(pyfile):
87 if hasdoctests(file):
88 # skip txt files that contain 'DISABLE_DOCTEST'
89 # this is for testing/evaluation, untested and might change
90 for line in open(file):
91 if line.lstrip().startswith('DISABLE_DOCTEST'):
92 return False
93 return True
95 return False
98 def get_test_suite(pkgname, files=[]):
99 """construct a test suite for a package
101 test suite will contain all doctests found somewhere in the package and
102 the tests passed as argument
104 1. get root folder for package
105 2. recurse and find everything that might contain a doctest
106 3. create the test suite
110 def testsuite(doctestfile):
111 if doctestfile.endswith('.txt'):
112 doctest = doctestunit.DocFileSuite(doctestfile,
113 package=pkgname,
114 setUp=setUp, tearDown=tearDown,
115 optionflags=optionflags,
117 return doctest
119 if doctestfile.endswith('.py'):
120 module = doctestfile.replace('.py','').replace(os.sep, '.')
121 module = '.'.join((pkgname, module,))
122 module = saneimport(module)
123 doctest = doctestunit.DocTestSuite(module,
124 setUp=setUp, tearDown=tearDown,
125 optionflags=optionflags,
127 return doctest
129 def fulltestsuite():
132 pkg = saneimport(pkgname)
133 path = pkgpath(pkg)
135 doctestfiles = recursedir(
136 path,
137 cond=ispackagedir,
138 filefilter=scanfordoctest,
140 # make relative to pkg path
141 doctestfiles = [x[len(path)+1:] for x in doctestfiles]
142 doctests = [testsuite(x) for x in doctestfiles+files]
144 test_suite = unittest.TestSuite(doctests)
145 return test_suite
147 return fulltestsuite