OpenSecurity/install/web.py-0.37/web/test.py
author om
Mon, 02 Dec 2013 14:02:05 +0100
changeset 3 65432e6c6042
permissions -rwxr-xr-x
initial deployment and project layout commit
om@3
     1
"""test utilities
om@3
     2
(part of web.py)
om@3
     3
"""
om@3
     4
import unittest
om@3
     5
import sys, os
om@3
     6
import web
om@3
     7
om@3
     8
TestCase = unittest.TestCase
om@3
     9
TestSuite = unittest.TestSuite
om@3
    10
om@3
    11
def load_modules(names):
om@3
    12
    return [__import__(name, None, None, "x") for name in names]
om@3
    13
om@3
    14
def module_suite(module, classnames=None):
om@3
    15
    """Makes a suite from a module."""
om@3
    16
    if classnames:
om@3
    17
        return unittest.TestLoader().loadTestsFromNames(classnames, module)
om@3
    18
    elif hasattr(module, 'suite'):
om@3
    19
        return module.suite()
om@3
    20
    else:
om@3
    21
        return unittest.TestLoader().loadTestsFromModule(module)
om@3
    22
om@3
    23
def doctest_suite(module_names):
om@3
    24
    """Makes a test suite from doctests."""
om@3
    25
    import doctest
om@3
    26
    suite = TestSuite()
om@3
    27
    for mod in load_modules(module_names):
om@3
    28
        suite.addTest(doctest.DocTestSuite(mod))
om@3
    29
    return suite
om@3
    30
    
om@3
    31
def suite(module_names):
om@3
    32
    """Creates a suite from multiple modules."""
om@3
    33
    suite = TestSuite()
om@3
    34
    for mod in load_modules(module_names):
om@3
    35
        suite.addTest(module_suite(mod))
om@3
    36
    return suite
om@3
    37
om@3
    38
def runTests(suite):
om@3
    39
    runner = unittest.TextTestRunner()
om@3
    40
    return runner.run(suite)
om@3
    41
om@3
    42
def main(suite=None):
om@3
    43
    if not suite:
om@3
    44
        main_module = __import__('__main__')
om@3
    45
        # allow command line switches
om@3
    46
        args = [a for a in sys.argv[1:] if not a.startswith('-')]
om@3
    47
        suite = module_suite(main_module, args or None)
om@3
    48
om@3
    49
    result = runTests(suite)
om@3
    50
    sys.exit(not result.wasSuccessful())
om@3
    51