~lifeless/python-oops-tools/bug-881400

« back to all changes in this revision

Viewing changes to src/oopstools/oops/test/test_runner.py

  • Committer: Robert Collins
  • Date: 2011-10-13 20:18:51 UTC
  • Revision ID: robertc@robertcollins.net-20111013201851-ym8jmdhoeol3p83s
Export of cruft-deleted tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2005-2011 Canonical Ltd.  All rights reserved.
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Affero General Public License as published by
 
5
# the Free Software Foundation, either version 3 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU Affero General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Affero General Public License
 
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
import doctest
 
17
import imp
 
18
import os.path
 
19
import re
 
20
import unittest
 
21
 
 
22
from glob import glob
 
23
from os import path
 
24
from django.test.simple import DjangoTestSuiteRunner
 
25
 
 
26
DEFAULT_OPTIONS = (
 
27
    doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS |doctest.REPORT_NDIFF)
 
28
 
 
29
class CustomTestRunner(DjangoTestSuiteRunner):
 
30
 
 
31
    def __init__(self, verbosity=3, interactive=False, failfast=True):
 
32
        super(CustomTestRunner, self).__init__(verbosity=verbosity,
 
33
            interactive=interactive, failfast=failfast)
 
34
 
 
35
    def build_suite(self, test_labels, extra_tests=None, **kwargs):
 
36
        # XXX: matsubara 2010-02-19 bug 524440: Adding a sort here to
 
37
        # guarantee we'll have the doctests in the same order. Ideally tests
 
38
        # should be contained and not need to be run on a specific order.
 
39
        doctests = sorted(glob(path.join(path.dirname(__file__), '*.txt')))
 
40
        filtered_doctests = set([])
 
41
        for label in test_labels:
 
42
            # regular expression match.
 
43
            test_name = re.compile(label)
 
44
            filtered_doctests.update(filter(test_name.search, doctests))
 
45
        # XXX: According to Python Library Reference:
 
46
        # Changed in version 2.5: The global __file__ was added to the
 
47
        # globals provided to doctests loaded from a text file using
 
48
        # DocFileSuite(). This is a hack to make tests work on 2.4
 
49
        globs = {'__file__': __file__}
 
50
        filtered_doctests = sorted(list(filtered_doctests))
 
51
        suite = doctest.DocFileSuite(module_relative=False,
 
52
                                     optionflags=DEFAULT_OPTIONS, globs=globs,
 
53
                                     *filtered_doctests)
 
54
 
 
55
        # Add the unit tests as well.
 
56
        unittests = sorted(
 
57
            glob(path.join(path.dirname(__file__), 'test_*.py')))
 
58
        unittests = filter(lambda x: x != __file__, unittests)
 
59
        for test in unittests:
 
60
            mod_name, ext = os.path.splitext(os.path.basename(test))
 
61
            mod_data = imp.find_module(mod_name, [os.path.dirname(test)])
 
62
            test_module = imp.load_module(mod_name, *mod_data)
 
63
            suite.addTest(
 
64
                unittest.defaultTestLoader.loadTestsFromModule(test_module))
 
65
        return suite