Skip to content
This repository
Browse code

new: test utilities

  • Loading branch information...
commit a44526987dbe1dc23b385897fb66a98eaa30397b 1 parent abe635e
Anand Chitipothu anandology authored

Showing 1 changed file with 51 additions and 0 deletions. Show diff stats Hide diff stats

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

0 comments on commit a445269

Please sign in to comment.
Something went wrong with that request. Please try again.