4
# Copyright (C) 2004 Canonical.com
5
# Author: Robert Collins <robert.collins@canonical.com>
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
from testtools.utils import iterate_tests
30
class ParameterisableTextTestRunner(unittest.TextTestRunner):
31
"""I am a TextTestRunner whose result class is
32
parameterisable without further subclassing"""
33
def __init__(self, **args):
34
unittest.TextTestRunner.__init__(self, **args)
35
self._resultFactory=None
36
def resultFactory(self, *args):
37
"""set or retrieve the result factory"""
39
self._resultFactory=args[0]
41
if self._resultFactory is None:
42
self._resultFactory=unittest._TextTestResult
43
return self._resultFactory
45
def _makeResult(self):
46
return self.resultFactory()(self.stream, self.descriptions, self.verbosity)
49
class EarlyStoppingTextTestResult(unittest._TextTestResult):
50
"""I am a TextTestResult that can optionally stop at the first failure
53
def addError(self, test, err):
54
unittest._TextTestResult.addError(self, test, err)
55
if self.stopOnError():
58
def addFailure(self, test, err):
59
unittest._TextTestResult.addError(self, test, err)
60
if self.stopOnFailure():
63
def stopOnError(self, *args):
64
"""should this result indicate an abort when an error occurs?
65
TODO parameterise this"""
68
def stopOnFailure(self, *args):
69
"""should this result indicate an abort when a failure error occurs?
70
TODO parameterise this"""
74
def earlyStopFactory(*args, **kwargs):
75
"""return a an early stopping text test result"""
76
result=EarlyStoppingTextTestResult(*args, **kwargs)
81
result = testscenarios.test_suite()
82
doctest.set_unittest_reportflags(doctest.REPORT_ONLY_FIRST_FAILURE)
83
result.addTest(doctest.DocFileSuite("README"))
88
"""To parameterise what tests are run, run this script like so:
89
python test_all.py REGEX
91
python test_all.py .*Protocol.*
92
to run all tests with Protocol in their id."""
95
suite = unittest.TestSuite()
96
filter = re.compile(pattern)
97
for test in iterate_tests(test_suite()):
98
if filter.search(test.id()):
102
runner = ParameterisableTextTestRunner(verbosity=2)
103
runner.resultFactory(earlyStopFactory)
104
if not runner.run(suite).wasSuccessful():
108
if __name__ == '__main__':
109
sys.exit(main(sys.argv))