Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
web.py test suite.
  • Loading branch information
anandology committed Dec 24, 2007
1 parent 3b9ca6c commit dd6e1af
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.tests
@@ -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


Empty file added test/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions test/alltests.py
@@ -0,0 +1,8 @@
import webtest

def suite():
modules = ["doctests", "db"]
return webtest.suite(modules)

if __name__ == "__main__":
webtest.main()
28 changes: 28 additions & 0 deletions test/db.py
@@ -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()
10 changes: 10 additions & 0 deletions test/doctests.py
@@ -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()
124 changes: 124 additions & 0 deletions test/webtest.py
@@ -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.