~testing-cabal/testscenarios/master

« back to all changes in this revision

Viewing changes to test_all.py

  • Committer: Robert Collins
  • Date: 2009-12-19 01:05:27 UTC
  • Revision ID: git-v1:290ae5cbc481fec6a68e8851c87bd31cafbcc31f
Start running tests using testtools.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- Mode: python -*-
3
 
#
4
 
# Copyright (C) 2004 Canonical.com 
5
 
#       Author:      Robert Collins <robert.collins@canonical.com>
6
 
#
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.
11
 
#
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.
16
 
#
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
20
 
#
21
 
 
22
 
import doctest
23
 
import unittest
24
 
import sys
25
 
import os
26
 
import shutil
27
 
 
28
 
from testtools.utils import iterate_tests
29
 
 
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"""
38
 
        if args:
39
 
            self._resultFactory=args[0]
40
 
            return self
41
 
        if self._resultFactory is None:
42
 
            self._resultFactory=unittest._TextTestResult
43
 
        return self._resultFactory
44
 
        
45
 
    def _makeResult(self):
46
 
        return self.resultFactory()(self.stream, self.descriptions, self.verbosity)
47
 
 
48
 
 
49
 
class EarlyStoppingTextTestResult(unittest._TextTestResult):
50
 
    """I am a TextTestResult that can optionally stop at the first failure
51
 
    or error"""
52
 
 
53
 
    def addError(self, test, err):
54
 
        unittest._TextTestResult.addError(self, test, err)
55
 
        if self.stopOnError():
56
 
            self.stop()
57
 
 
58
 
    def addFailure(self, test, err):
59
 
        unittest._TextTestResult.addError(self, test, err)
60
 
        if self.stopOnFailure():
61
 
            self.stop()
62
 
 
63
 
    def stopOnError(self, *args):
64
 
        """should this result indicate an abort when an error occurs?
65
 
        TODO parameterise this"""
66
 
        return True
67
 
 
68
 
    def stopOnFailure(self, *args):
69
 
        """should this result indicate an abort when a failure error occurs?
70
 
        TODO parameterise this"""
71
 
        return True
72
 
 
73
 
 
74
 
def earlyStopFactory(*args, **kwargs):
75
 
    """return a an early stopping text test result"""
76
 
    result=EarlyStoppingTextTestResult(*args, **kwargs)
77
 
    return result
78
 
 
79
 
def test_suite():
80
 
    import testscenarios
81
 
    result = testscenarios.test_suite()
82
 
    doctest.set_unittest_reportflags(doctest.REPORT_ONLY_FIRST_FAILURE)
83
 
    result.addTest(doctest.DocFileSuite("README"))
84
 
    return result
85
 
 
86
 
 
87
 
def main(argv):
88
 
    """To parameterise what tests are run, run this script like so:
89
 
    python test_all.py REGEX
90
 
    i.e.
91
 
    python test_all.py .*Protocol.*
92
 
    to run all tests with Protocol in their id."""
93
 
    if len(argv) > 1:
94
 
        pattern = argv[1]
95
 
        suite = unittest.TestSuite()
96
 
        filter = re.compile(pattern)
97
 
        for test in iterate_tests(test_suite()):
98
 
            if filter.search(test.id()):
99
 
                suite.addTest(test)
100
 
    else:
101
 
        suite = test_suite()
102
 
    runner = ParameterisableTextTestRunner(verbosity=2)
103
 
    runner.resultFactory(earlyStopFactory)
104
 
    if not runner.run(suite).wasSuccessful():
105
 
        return 1
106
 
    return 0
107
 
 
108
 
if __name__ == '__main__':
109
 
    sys.exit(main(sys.argv))