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
23 from subunit
.tests
.TestUtil
import TestVisitor
, TestSuite
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
)
80 class ShellTests(subunit
.ExecTestCase
):
82 def test_sourcing(self
):
83 """./shell/tests/test_source_library.sh"""
85 def test_functions(self
):
86 """./shell/tests/test_function_output.sh"""
91 result
.addTest(subunit
.test_suite())
92 result
.addTest(ShellTests('test_sourcing'))
93 result
.addTest(ShellTests('test_functions'))
97 class filteringVisitor(TestVisitor
):
98 """I accrue all the testCases I visit that pass a regexp filter on id
102 def __init__(self
, filter):
104 TestVisitor
.__init
__(self
)
106 self
.filter=re
.compile(filter)
109 """answer the suite we are building"""
110 if self
._suite
is None:
111 self
._suite
=TestSuite()
114 def visitCase(self
, aCase
):
115 if self
.filter.match(aCase
.id()):
116 self
.suite().addTest(aCase
)
120 """To parameterise what tests are run, run this script like so:
121 python test_all.py REGEX
123 python test_all.py .*Protocol.*
124 to run all tests with Protocol in their id."""
129 visitor
= filteringVisitor(pattern
)
130 test_suite().visit(visitor
)
131 runner
= ParameterisableTextTestRunner(verbosity
=2)
132 runner
.resultFactory(unittest
._TextTestResult
)
133 if not runner
.run(visitor
.suite()).wasSuccessful():
137 if __name__
== '__main__':
138 sys
.exit(main(sys
.argv
))