paula.testing: tests pass, no app dep, currently no zcml
[paula.git] / paula.testing / src / paula / testing / testing.py
blobb2ed29b37c9abed791961a537bdeda2af38a86b3
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/>.
18 """
19 """
20 __author__ = "Florian Friesdorf <flo@chaoflow.net>"
21 __docformat__ = "plaintext"
23 import os
24 import os.path
25 import types
26 import unittest
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
41 optionflags = \
42 doctest.REPORT_ONLY_FIRST_FAILURE + \
43 doctest.ELLIPSIS + \
44 doctest.NORMALIZE_WHITESPACE
47 def setUp(test):
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
50 contains doctests.
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.
54 """
55 testing.setUp(test)
56 for k,v in test_globs.items():
57 test.globs[k] = v
59 def tearDown(test):
60 """
61 """
62 testing.tearDown(test)
64 def scanfordoctest(file):
65 """Decides whether a file should be scanned for doctests
67 - all .py files
68 - all .txt files for which a .py file exists
69 """
70 # XXX: not reimplemented
71 # # skip if it starts with a dot
72 # if item.startswith('.'):
73 # continue
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)
83 return False
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
96 """
98 def testsuite(doctestfile):
99 if doctestfile.endswith('.txt'):
100 doctest = doctestunit.DocFileSuite(doctestfile,
101 package=pkgname,
102 setUp=setUp, tearDown=tearDown,
103 optionflags=optionflags,
105 return doctest
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,
115 return doctest
117 def fulltestsuite():
120 pkg = saneimport(pkgname)
121 path = pkgpath(pkg)
123 doctestfiles = recursedir(
124 path,
125 cond=ispackagedir,
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)
133 return test_suite
135 return fulltestsuite