~free.ekanayaka/storm/any-expr

« back to all changes in this revision

Viewing changes to test

  • Committer: Jamu Kakar
  • Date: 2009-11-24 18:34:34 UTC
  • mfrom: (340 storm.trunk)
  • mto: This revision was merged to the branch mainline in revision 341.
  • Revision ID: jkakar@kakar.ca-20091124183434-ambkvw9tp2byvjnc
- Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import optparse
23
23
import unittest
24
24
import doctest
25
 
import new
26
25
import sys
27
26
import os
28
27
 
35
34
    @param testpaths: If provided, only tests in the given sequence will
36
35
                      be considered.  If not provided, all tests are
37
36
                      considered.
38
 
    @return: (unittests, doctests) tuple, with lists of unittests and
39
 
             doctests found, respectively.
 
37
    @return: a test suite containing the requested tests.
40
38
    """
 
39
    suite = unittest.TestSuite()
41
40
    topdir = os.path.abspath(os.path.dirname(__file__))
42
41
    testdir = os.path.dirname(tests.__file__)
43
42
    testpaths = set(testpaths)
44
 
    unittests = []
45
 
    doctests = []
46
43
    for root, dirnames, filenames in os.walk(testdir):
47
44
        for filename in filenames:
48
45
            filepath = os.path.join(root, filename)
62
59
                    continue
63
60
 
64
61
            if filename.endswith(".py"):
65
 
                unittests.append(relpath)
66
 
            elif relpath == os.path.join("tests", "zope", "README.txt"):
67
 
                # Special case the inclusion of the Zope-dependent
68
 
                # ZStorm doctest.
69
 
                from tests.zope import has_zope
70
 
                if has_zope:
71
 
                    doctests.append(relpath)
 
62
                modpath = relpath.replace(os.path.sep, ".")[:-3]
 
63
                module = __import__(modpath, None, None, [""])
 
64
                suite.addTest(
 
65
                    unittest.defaultTestLoader.loadTestsFromModule(module))
72
66
            elif filename.endswith(".txt"):
73
 
                doctests.append(relpath)
 
67
                load_test = True
 
68
                if relpath == os.path.join("tests", "zope", "README.txt"):
 
69
                    # Special case the inclusion of the Zope-dependent
 
70
                    # ZStorm doctest.
 
71
                    from tests.zope import has_zope
 
72
                    load_test = has_zope
 
73
                if load_test:
 
74
                    parent_path = os.path.dirname(relpath).replace(
 
75
                        os.path.sep, ".")
 
76
                    parent_module = __import__(parent_path, None, None, [""])
 
77
                    suite.addTest(doctest.DocFileSuite(
 
78
                            os.path.basename(relpath),
 
79
                            module_relative=True,
 
80
                            package=parent_module,
 
81
                            optionflags=doctest.ELLIPSIS))
74
82
 
75
 
    return unittests, doctests
 
83
    return suite
76
84
 
77
85
 
78
86
def parse_sys_argv():
85
93
            del sys.argv[i]
86
94
    return testpaths
87
95
 
88
 
 
89
 
def test_with_trial():
90
 
    from twisted.scripts import trial
91
 
    unittests, doctests = find_tests(parse_sys_argv())
92
 
    sys.argv.extend(unittests)
93
 
    trial.run()
94
 
 
95
 
 
96
 
def test_with_py_test():
97
 
    import py
98
 
    dirname = os.path.dirname(__file__)
99
 
    unittests, doctests = find_tests(parse_sys_argv())
100
 
    sys.argv.extend(unittests)
101
 
    sys.argv.extend(doctests)
102
 
    # For timestamp checking when looping:
103
 
    sys.argv.append(os.path.join(os.path.dirname(__file__), "storm/"))
104
 
    py.test.cmdline.main()
105
 
 
106
 
 
107
 
def test_with_unittest():
108
 
 
 
96
def test_with_runner(runner):
109
97
    usage = "test.py [options] [<test filename>, ...]"
110
98
 
111
99
    parser = optparse.OptionParser(usage=usage)
113
101
    parser.add_option('--verbose', action='store_true')
114
102
    opts, args = parser.parse_args()
115
103
 
116
 
    runner = unittest.TextTestRunner()
117
 
 
118
104
    if opts.verbose:
119
105
        runner.verbosity = 2
120
106
 
121
 
    loader = unittest.TestLoader()
122
 
 
123
 
    unittests, doctests = find_tests(args)
124
 
 
125
 
    class Summary:
126
 
        def __init__(self):
127
 
            self.total_failures = 0
128
 
            self.total_errors = 0
129
 
            self.total_tests = 0
130
 
        def __call__(self, tests, failures, errors):
131
 
            self.total_tests += tests
132
 
            self.total_failures += failures
133
 
            self.total_errors += errors
134
 
            sys.stderr.write("(tests=%d, failures=%d, errors=%d)\n" %
135
 
                             (tests, failures, errors))
136
 
 
137
 
    unittest_summary = Summary()
138
 
    doctest_summary = Summary()
139
 
 
140
 
    if unittests:
141
 
        sys.stderr.write("Running unittests...\n")
142
 
        for relpath in unittests:
143
 
            sys.stderr.write("[%s]\n" % relpath)
144
 
            modpath = relpath.replace(os.path.sep, '.')[:-3]
145
 
            module = __import__(modpath, None, None, [""])
146
 
            test = loader.loadTestsFromModule(module)
147
 
            result = runner.run(test)
148
 
            unittest_summary(test.countTestCases(),
149
 
                             len(result.failures), len(result.errors))
150
 
            sys.stderr.write("\n")
151
 
 
152
 
    if doctests:
153
 
        sys.stderr.write("Running doctests...\n")
154
 
        doctest_flags = doctest.ELLIPSIS
155
 
        for relpath in doctests:
156
 
            sys.stderr.write("[%s]\n" % relpath)
157
 
            failures, total = doctest.testfile(relpath,
158
 
                                               optionflags=doctest_flags)
159
 
            doctest_summary(total, failures, 0)
160
 
            sys.stderr.write("\n")
161
 
 
162
 
    sys.stderr.write("Total test cases: %d\n" % unittest_summary.total_tests)
163
 
    sys.stderr.write("Total doctests: %d\n" % doctest_summary.total_tests)
164
 
    sys.stderr.write("Total failures: %d\n" %
165
 
                     (unittest_summary.total_failures +
166
 
                      doctest_summary.total_failures))
167
 
    sys.stderr.write("Total errors: %d\n" % (unittest_summary.total_errors +
168
 
                                             doctest_summary.total_errors))
169
 
 
170
 
    failed = bool(unittest_summary.total_failures or
171
 
                  unittest_summary.total_errors or
172
 
                  doctest_summary.total_failures or
173
 
                  doctest_summary.total_errors)
174
 
 
175
 
    sys.exit(failed)
 
107
    suite = find_tests(args)
 
108
    result = runner.run(suite)
 
109
    return not result.wasSuccessful()
 
110
 
 
111
 
 
112
def test_with_trial():
 
113
    from twisted.trial.reporter import TreeReporter
 
114
    from twisted.trial.runner import TrialRunner
 
115
    runner = TrialRunner(reporterFactory=TreeReporter, realTimeErrors=True)
 
116
    return test_with_runner(runner)
 
117
 
 
118
 
 
119
def test_with_unittest():
 
120
    runner = unittest.TextTestRunner()
 
121
    return test_with_runner(runner)
 
122
 
176
123
 
177
124
if __name__ == "__main__":
178
125
    runner = os.environ.get("STORM_TEST_RUNNER")
181
128
    runner_func = globals().get("test_with_%s" % runner.replace(".", "_"))
182
129
    if not runner_func:
183
130
        sys.exit("Test runner not found: %s" % runner)
184
 
    runner_func()
 
131
    sys.exit(runner_func())
185
132
 
186
133
# vim:ts=4:sw=4:et