~jseutter/storm/py3_exceptions

« back to all changes in this revision

Viewing changes to tests/__init__.py

Merge oneiric-admin-shutdown-bug-871596 [f=871596] [r=free,rvb,stub]

- Switch to setuptools.
- Improve disconnection error detection for PostgreSQL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (c) 2011 Canonical
 
3
#
 
4
# Written by Gustavo Niemeyer <gustavo@niemeyer.net>
 
5
#
 
6
# This file is part of Storm Object Relational Mapper.
 
7
#
 
8
# Storm is free software; you can redistribute it and/or modify
 
9
# it under the terms of the GNU Lesser General Public License as
 
10
# published by the Free Software Foundation; either version 2.1 of
 
11
# the License, or (at your option) any later version.
 
12
#
 
13
# Storm is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU Lesser General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU Lesser General Public License
 
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
 
 
22
__all__ = [
 
23
    'find_tests',
 
24
    'has_fixtures',
 
25
    ]
 
26
 
 
27
import doctest
 
28
import os
 
29
import unittest
 
30
 
 
31
try:
 
32
    import fixtures
 
33
    fixtures  # Silence lint.
 
34
except ImportError:
 
35
    has_fixtures = False
 
36
else:
 
37
    has_fixtures = True
 
38
 
 
39
 
 
40
def find_tests(testpaths=()):
 
41
    """Find all test paths, or test paths contained in the provided sequence.
 
42
 
 
43
    @param testpaths: If provided, only tests in the given sequence will
 
44
                      be considered.  If not provided, all tests are
 
45
                      considered.
 
46
    @return: a test suite containing the requested tests.
 
47
    """
 
48
    suite = unittest.TestSuite()
 
49
    topdir = os.path.abspath(
 
50
        os.path.join(os.path.dirname(__file__), os.pardir))
 
51
    testdir = os.path.dirname(__file__)
 
52
    testpaths = set(testpaths)
 
53
    for root, dirnames, filenames in os.walk(testdir):
 
54
        for filename in filenames:
 
55
            filepath = os.path.join(root, filename)
 
56
            relpath = filepath[len(topdir)+1:]
 
57
 
 
58
            if (filename == "__init__.py" or filename.endswith(".pyc") or
 
59
                relpath == os.path.join("tests", "conftest.py")):
 
60
                # Skip non-tests.
 
61
                continue
 
62
 
 
63
            if testpaths:
 
64
                # Skip any tests not in testpaths.
 
65
                for testpath in testpaths:
 
66
                    if relpath.startswith(testpath):
 
67
                        break
 
68
                else:
 
69
                    continue
 
70
 
 
71
            if filename.endswith(".py"):
 
72
                modpath = relpath.replace(os.path.sep, ".")[:-3]
 
73
                module = __import__(modpath, None, None, [""])
 
74
                suite.addTest(
 
75
                    unittest.defaultTestLoader.loadTestsFromModule(module))
 
76
            elif filename.endswith(".txt"):
 
77
                load_test = True
 
78
                if relpath == os.path.join("tests", "zope", "README.txt"):
 
79
                    # Special case the inclusion of the Zope-dependent
 
80
                    # ZStorm doctest.
 
81
                    import tests.zope as ztest
 
82
                    load_test = (
 
83
                        ztest.has_transaction and
 
84
                        ztest.has_zope_component and
 
85
                        ztest.has_zope_security)
 
86
                if load_test:
 
87
                    parent_path = os.path.dirname(relpath).replace(
 
88
                        os.path.sep, ".")
 
89
                    parent_module = __import__(parent_path, None, None, [""])
 
90
                    suite.addTest(doctest.DocFileSuite(
 
91
                            os.path.basename(relpath),
 
92
                            module_relative=True,
 
93
                            package=parent_module,
 
94
                            optionflags=doctest.ELLIPSIS))
 
95
 
 
96
    return suite