Permalink
Browse files

web.py test suite.

  • Loading branch information...
anandology committed Dec 24, 2007
1 parent 3b9ca6c commit dd6e1af5451243b26009cfbea72b9968eec69cf7
Showing with 197 additions and 0 deletions.
  1. +27 −0 README.tests
  2. 0 test/__init__.py
  3. +8 −0 test/alltests.py
  4. +28 −0 test/db.py
  5. +10 −0 test/doctests.py
  6. +124 −0 test/webtest.py
View
@@ -0,0 +1,27 @@
+# web.py unit tests
+
+## Setup
+
+All databases expect a database with name `webpy` with username `scott` and password `tiger`.
+
+## Running all tests
+
+To run all tests:
+
+ $ python test/alltests.py
+
+## Running individual tests
+
+To run all tests in a file:
+
+ $ python test/db.py
+
+To run all tests in a class:
+
+ $ python test/db.py SqliteTest
+
+To run a single test:
+
+ $ python test/db.py SqliteTest.testUnicode
+
+
View
No changes.
View
@@ -0,0 +1,8 @@
+import webtest
+
+def suite():
+ modules = ["doctests", "db"]
+ return webtest.suite(modules)
+
+if __name__ == "__main__":
+ webtest.main()
View
@@ -0,0 +1,28 @@
+"""DB test"""
+import webtest
+import web
+
+class DBTest(webtest.TestCase):
+ dbname = 'postgres'
+
+ def setUpAll(self):
+ self.db = webtest.setup_database(self.dbname)
+ self.db.query("CREATE TABLE person (name text, email text)")
+
+ def tearDownAll(self):
+ # there might be some error with the current connection, delete from a new connection
+ self.db = webtest.setup_database(self.dbname)
+ self.db.query('DROP TABLE person')
+
+ def testUnicode(self):
+ """Bug#177265: unicode queries throw errors"""
+ self.db.select('person', where='name=$name', vars={'name': u'\xf4'})
+
+class SqliteTest(DBTest):
+ dbname = "sqlite"
+
+class MySQLTest(DBTest):
+ dbname = "mysql"
+
+if __name__ == '__main__':
+ webtest.main()
View
@@ -0,0 +1,10 @@
+"""Run all doctests in web.py.
+"""
+import webtest
+
+def suite():
+ modules = ["web.utils", "web.db", "web.net", "web.wsgi", "web.http", "web.webapi", "web.request"]
+ return webtest.doctest_suite(modules)
+
+if __name__ == "__main__":
+ webtest.main()
View
@@ -0,0 +1,124 @@
+"""webtest: test utilities.
+"""
+import unittest
+import sys, os
+
+# adding current directory to path to make sure local copy of web module is used.
+sys.path.insert(0, '.')
+
+import web
+
+
+class TestCase(unittest.TestCase):
+ def setUpAll(self):
+ pass
+
+ def tearDownAll(self):
+ pass
+
+ def shortDescription(self):
+ """overridden to not return docstrings"""
+ return None
+
+class TestSuite(unittest.TestSuite):
+ """A TestSuite with once per TestCase setUpAll() and tearDownAll().
+ Adopted from test/testlib/testing.py file in SQLAlchemy test suite.
+ """
+
+ def __init__(self, tests=()):
+ if len(tests) >0 and isinstance(tests[0], TestCase):
+ self._initTest = tests[0]
+ else:
+ self._initTest = None
+ unittest.TestSuite.__init__(self, tests)
+
+ def do_run(self, result):
+ # nice job unittest ! you switched __call__ and run() between py2.3
+ # and 2.4 thereby making straight subclassing impossible !
+ for test in self._tests:
+ if result.shouldStop:
+ break
+ test(result)
+ return result
+
+ def run(self, result):
+ return self(result)
+
+ def __call__(self, result):
+ try:
+ if self._initTest is not None:
+ self._initTest.setUpAll()
+ except:
+ # skip tests if global setup fails
+ ex = self.__exc_info()
+ for test in self._tests:
+ result.addError(test, ex)
+ return False
+ try:
+ return self.do_run(result)
+ finally:
+ try:
+ if self._initTest is not None:
+ self._initTest.tearDownAll()
+ except:
+ result.addError(self._initTest, self.__exc_info())
+ pass
+
+ def __exc_info(self):
+ """Return a version of sys.exc_info() with the traceback frame
+ minimised; usually the top level of the traceback frame is not
+ needed.
+ ripped off out of unittest module since its double __
+ """
+ exctype, excvalue, tb = sys.exc_info()
+ if sys.platform[:4] == 'java': ## tracebacks look different in Jython
+ return (exctype, excvalue, tb)
+ return (exctype, excvalue, tb)
+
+# monkeypatch
+unittest.TestLoader.suiteClass = TestSuite
+
+def runTests(suite):
+ runner = unittest.TextTestRunner()
+ return runner.run(suite)
+
+def main(suite=None):
+ if not suite:
+ main_module = __import__('__main__')
+ suite = module_suite(main_module, sys.argv[1:] or None)
+
+ result = runTests(suite)
+ sys.exit(not result.wasSuccessful())
+
+def load_modules(names):
+ return [__import__(name, None, None, "x") for name in names]
+
+def module_suite(module, classnames=None):
+ """Makes a suite from a module."""
+ if hasattr(module, 'suite'):
+ return module.suite()
+ elif classnames:
+ return unittest.TestLoader().loadTestsFromNames(classnames, module)
+ else:
+ return unittest.TestLoader().loadTestsFromModule(module)
+
+def doctest_suite(module_names):
+ """Makes a test suite from doctests."""
+ import doctest
+ suite = unittest.TestSuite()
+ for mod in load_modules(module_names):
+ suite.addTest(doctest.DocTestSuite(mod))
+ return suite
+
+def suite(module_names):
+ """Creates a suite from multiple modules."""
+ suite = unittest.TestSuite()
+ for mod in load_modules(module_names):
+ suite.addTest(module_suite(mod))
+ return suite
+
+def setup_database(dbname):
+ if dbname == 'sqlite':
+ return web.database(dbn=dbname, db='webpy.db')
+ else:
+ return web.database(dbn=dbname, db='webpy', user='scott', pw='tiger')

0 comments on commit dd6e1af

Please sign in to comment.