~javier.collado/checkbox-core/bug991875

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
#
# This file is part of Checkbox.
#
# Copyright 2012 Canonical Ltd.
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
#
import os
import sys
import glob
import logging

import optparse
import doctest
import unittest


def test_with_runner(runner):
    usage = "test.py [options] [<test filename>, ...]"

    parser = optparse.OptionParser(usage=usage)

    parser.add_option('--log-level')
    parser.add_option('--verbose', action='store_true')
    options, args = parser.parse_args()

    if options.log_level:
        log_level = logging.getLevelName(options.log_level.upper())
        if log_level:
            logger = logging.getLogger()
            logger.setLevel(log_level)

    if options.verbose:
        runner.verbosity = 2

    # Import late, after any and all sys.path jiggery pokery.
    from checkbox.tests import find_tests

    suite = find_tests(args)
    result = runner.run(suite)
    return not result.wasSuccessful()


def test_with_trial():
    from twisted.trial.reporter import TreeReporter
    from twisted.trial.runner import TrialRunner

    runner = TrialRunner(reporterFactory=TreeReporter, realTimeErrors=True)
    return test_with_runner(runner)


def test_with_unittest():
    runner = unittest.TextTestRunner()
    return test_with_runner(runner)


if __name__ == "__main__":
    runner = os.environ.get("TEST_RUNNER")
    if not runner:
        runner = "unittest"
    runner_func = globals().get("test_with_%s" % runner.replace(".", "_"))
    if not runner_func:
        sys.exit("Test runner not found: %s" % runner)
    sys.exit(runner_func())