~vcs-imports/clientcookie/trunk

« back to all changes in this revision

Viewing changes to test.py

  • Committer: jjlee
  • Date: 2004-03-31 19:36:11 UTC
  • Revision ID: svn-v4:fd0d7bf2-dfb6-0310-8d31-b7ecfe96aada:user/jjlee/wwwsearch/ClientCookie:3571
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""Test runner.
 
4
 
 
5
To run tests against installed code rather than files extracted from package,
 
6
use the -i command line option.
 
7
 
 
8
For further help, enter this at a command prompt:
 
9
 
 
10
python test.py --help
 
11
 
 
12
"""
 
13
 
 
14
# Modules containing tests to run -- a test is anything named *Tests, which
 
15
# should be classes deriving from unittest.TestCase.
 
16
MODULE_NAMES = ["test_misc", "test_date", "test_headers", "test_cookies",
 
17
                "test_urllib2"]
 
18
 
 
19
import sys, os, traceback
 
20
from unittest import TestCase
 
21
 
 
22
try: True
 
23
except NameError:
 
24
    True = 1
 
25
    False = 0
 
26
 
 
27
#import ClientCookie
 
28
#ClientCookie.CLIENTCOOKIE_DEBUG = True
 
29
#level = ClientCookie.DEBUG
 
30
#level = ClientCookie.INFO
 
31
#level = ClientCookie.NOTSET
 
32
#ClientCookie.getLogger("ClientCookie").setLevel(level)
 
33
 
 
34
RUN_AGAINST_INSTALLED = False
 
35
 
 
36
def import_tests(module_names):
 
37
    """Import everything named *Tests from named modules.
 
38
 
 
39
    This is so unittest.main() will run test cases in other modules.
 
40
 
 
41
    """
 
42
    try:
 
43
        from ClientCookie._Util import endswith
 
44
    except ImportError:
 
45
        traceback.print_exc()
 
46
        if RUN_AGAINST_INSTALLED:
 
47
            sys.exit("Perhaps ClientCookie isn't installed properly?")
 
48
        else:
 
49
            sys.exit("Perhaps the locally extracted source files aren't in "
 
50
                     "this directory?")
 
51
 
 
52
    g = globals()
 
53
    for module_name in module_names:
 
54
        try:
 
55
            __import__(module_name)
 
56
        except ImportError:
 
57
            traceback.print_exc()
 
58
            sys.exit("Import of test module failed -- Couldn't find tests?")
 
59
        module = sys.modules[module_name]
 
60
        candidates = dir(module)
 
61
        for name in candidates:
 
62
            if endswith(name, "Tests"):
 
63
                test_class = getattr(module, name)
 
64
                g[name] = test_class
 
65
 
 
66
 
 
67
if __name__ == "__main__":
 
68
    import unittest
 
69
    test_path = os.path.join(os.path.dirname(sys.argv[0]), "test")
 
70
    try:
 
71
        i = sys.argv.index("-i")
 
72
    except ValueError:
 
73
        RUN_AGAINST_INSTALLED = False
 
74
        sys.path.insert(0, test_path)
 
75
    else:
 
76
        del sys.argv[i]
 
77
        RUN_AGAINST_INSTALLED = True
 
78
        sys.path[0] = test_path
 
79
    import_tests(MODULE_NAMES)
 
80
    unittest.main()