~ahasenack/landscape-client/landscape-client-1.5.5-0ubuntu0.9.04.0

« back to all changes in this revision

Viewing changes to test

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-09-08 16:35:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908163557-l3ixzj5dxz37wnw2
Tags: 1.0.18-0ubuntu1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import tempfile
 
3
import optparse
 
4
import unittest
 
5
import doctest
 
6
import shutil
 
7
import new
 
8
import sys
 
9
import os
 
10
 
 
11
lib_dir = os.path.abspath(os.path.dirname(__file__))
 
12
sys.path.insert(0, lib_dir)
 
13
 
 
14
# This is needed for DBUS listeners to work.
 
15
from twisted.internet.glib2reactor import install
 
16
install()
 
17
 
 
18
import landscape
 
19
 
 
20
 
 
21
def find_tests(testpaths=()):
 
22
    """Find all test paths, or test paths contained in the provided sequence.
 
23
 
 
24
    @param testpaths: If provided, only tests in the given sequence will
 
25
                      be considered.  If not provided, all tests are
 
26
                      considered.
 
27
    @return: (unittests, doctests) tuple, with lists of unittests and
 
28
             doctests found, respectively.
 
29
    """
 
30
    topdir = os.path.abspath(os.path.dirname(__file__))
 
31
    testdir = os.path.dirname(landscape.__file__)
 
32
    testpaths = set(testpaths)
 
33
    unittests = []
 
34
    doctests = []
 
35
    for root, dirnames, filenames in os.walk(testdir):
 
36
        for filename in filenames:
 
37
            filepath = os.path.join(root, filename)
 
38
            relpath = filepath[len(topdir)+1:]
 
39
 
 
40
            if (filename == "__init__.py" or filename.endswith(".pyc") or
 
41
                relpath == "landscape/conftest.py" or "/tests/" not in relpath):
 
42
                # Skip non-tests.
 
43
                continue
 
44
 
 
45
            if testpaths:
 
46
                # Skip any tests not in testpaths.
 
47
                for testpath in testpaths:
 
48
                    if relpath.startswith(testpath):
 
49
                        break
 
50
                else:
 
51
                    continue
 
52
 
 
53
            if filename.endswith(".py"):
 
54
                unittests.append(relpath)
 
55
            elif filename.endswith(".txt"):
 
56
                doctests.append(relpath)
 
57
 
 
58
    return unittests, doctests
 
59
 
 
60
 
 
61
def parse_sys_argv():
 
62
    """Extract any arguments not starting with '-' from sys.argv."""
 
63
    testpaths = []
 
64
    for i in range(len(sys.argv)-1,0,-1):
 
65
        arg = sys.argv[i]
 
66
        if not arg.startswith("-"):
 
67
            testpaths.append(arg)
 
68
            del sys.argv[i]
 
69
    return testpaths
 
70
 
 
71
 
 
72
def test_with_trial():
 
73
    from twisted.scripts import trial
 
74
    unittests, doctests = find_tests(parse_sys_argv())
 
75
    sys.argv.extend(unittests)
 
76
    trial.run()
 
77
 
 
78
 
 
79
def test_with_py_test():
 
80
    import py
 
81
    dirname = os.path.dirname(__file__)
 
82
    unittests, doctests = find_tests(parse_sys_argv())
 
83
    sys.argv.extend(unittests)
 
84
    sys.argv.extend(doctests)
 
85
    # For timestamp checking when looping:
 
86
    sys.argv.append(os.path.join(os.path.dirname(__file__), "landscape/"))
 
87
    py.test.cmdline.main()
 
88
 
 
89
 
 
90
def test_with_unittest():
 
91
 
 
92
    usage = "test.py [options] [<test filename>, ...]"
 
93
 
 
94
    parser = optparse.OptionParser(usage=usage)
 
95
 
 
96
    parser.add_option('--verbose', action='store_true')
 
97
    opts, args = parser.parse_args()
 
98
 
 
99
    runner = unittest.TextTestRunner()
 
100
 
 
101
    if opts.verbose:
 
102
        runner.verbosity = 2
 
103
 
 
104
    loader = unittest.TestLoader()
 
105
 
 
106
    unittests, doctests = find_tests(args)
 
107
 
 
108
    class Summary:
 
109
        def __init__(self):
 
110
            self.total_failures = 0
 
111
            self.total_errors = 0
 
112
            self.total_tests = 0
 
113
        def __call__(self, tests, failures, errors):
 
114
            self.total_tests += tests
 
115
            self.total_failures += failures
 
116
            self.total_errors += errors
 
117
            print "(tests=%d, failures=%d, errors=%d)" % \
 
118
                  (tests, failures, errors)
 
119
 
 
120
    unittest_summary = Summary()
 
121
    doctest_summary = Summary()
 
122
 
 
123
    if unittests:
 
124
        print "Running unittests..."
 
125
        for relpath in unittests:
 
126
            print "[%s]" % relpath
 
127
            modpath = relpath.replace('/', '.')[:-3]
 
128
            module = __import__(modpath, None, None, [""])
 
129
            test = loader.loadTestsFromModule(module)
 
130
            result = runner.run(test)
 
131
            unittest_summary(test.countTestCases(),
 
132
                             len(result.failures), len(result.errors))
 
133
            print
 
134
 
 
135
    if doctests:
 
136
        print "Running doctests..."
 
137
        doctest_flags = doctest.ELLIPSIS
 
138
        for relpath in doctests:
 
139
            print "[%s]" % relpath
 
140
            failures, total = doctest.testfile(relpath,
 
141
                                               optionflags=doctest_flags)
 
142
            doctest_summary(total, failures, 0)
 
143
            print
 
144
 
 
145
    print "Total test cases: %d" % unittest_summary.total_tests
 
146
    print "Total doctests: %d" % doctest_summary.total_tests
 
147
    print "Total failures: %d" % (unittest_summary.total_failures +
 
148
                                  doctest_summary.total_failures)
 
149
    print "Total errors: %d" % (unittest_summary.total_errors +
 
150
                                doctest_summary.total_errors)
 
151
 
 
152
    failed = bool(unittest_summary.total_failures or
 
153
                  unittest_summary.total_errors or
 
154
                  doctest_summary.total_failures or
 
155
                  doctest_summary.total_errors)
 
156
 
 
157
    sys.exit(failed)
 
158
 
 
159
if __name__ == "__main__":
 
160
    runner = os.environ.get("LANDSCAPE_TEST_RUNNER")
 
161
    if not runner:
 
162
        runner = "unittest"
 
163
    runner_func = globals().get("test_with_%s" % runner.replace(".", "_"))
 
164
    if not runner_func:
 
165
        sys.exit("Test runner not found: %s" % runner)
 
166
    runner_func()
 
167
 
 
168
# vim:ts=4:sw=4:et