use unittest.TestCase for tests

This commit is contained in:
iElectric 2009-06-05 00:41:05 +00:00
parent 31bd33bcff
commit 4356e8b582
4 changed files with 77 additions and 96 deletions

View File

@ -1,14 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import sys
## Append test method name,etc. to descriptions automatically.
## Yes, this is ugly, but it's the simplest way...
#def getDescription(self,test):
# ret = str(test)
# if self.descriptions:
# ret += "\n\t"+(test.shortDescription() or '')
# return ret
#unittest._TextTestResult.getDescription = getDescription
# Append test method name,etc. to descriptions automatically.
# Yes, this is ugly, but it's the simplest way...
def getDescription(self, test):
ret = str(test)
if self.descriptions:
return test.shortDescription() or ret
return ret
unittest._TextTestResult.getDescription = getDescription
class Result(unittest._TextTestResult):
# test description may be changed as we go; store the description at
@ -59,4 +64,3 @@ from base import Base
from pathed import Pathed
from shell import Shell
from database import DB,usedb

View File

@ -1,44 +1,23 @@
#import unittest
#from py.test import raises
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from nose.tools import raises, eq_
class FakeTestCase(object):
"""Mimics unittest.testcase methods
Minimize changes needed in migration to py.test
"""
def setUp(self):
pass
class Base(unittest.TestCase):
def setup_method(self,func=None):
self.setUp()
def tearDown(self):
pass
def teardown_method(self,func=None):
self.tearDown()
def assert_(self,x,doc=None):
assert x
def assertEquals(self,x,y,doc=None):
eq_(x, y)
def assertNotEquals(self,x,y,doc=None):
assert x != y
def assertRaises(self, exceptions, func, *arg, **kw):
if not hasattr(exceptions, '__iter__'):
exceptions = (exceptions, )
valid = ' or '.join([e.__name__ for e in exceptions])
try:
func(*arg, **kw)
except exceptions:
pass
except:
raise
else:
message = "%s() did not raise %s" % (func.__name__, valid)
raise AssertionError(message)
def assertEqualsIgnoreWhitespace(self, v1, v2):
"""Compares two strings that should be\
identical except for whitespace
"""
def createLines(s):
s = s.replace(' ', '')
lines = s.split('\n')
@ -49,9 +28,6 @@ class FakeTestCase(object):
for line1, line2 in zip(lines1, lines2):
self.assertEquals(line1, line2)
class Base(FakeTestCase):
"""Base class for other test cases"""
def ignoreErrors(self, func, *p,**k):
"""Call a function, ignoring any exceptions"""
try:

View File

@ -1,21 +1,29 @@
from base import Base
from pathed import Pathed
from sqlalchemy import create_engine, Table, MetaData
from sqlalchemy.orm import create_session
from pkg_resources import resource_stream
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from sqlalchemy import create_engine, Table, MetaData
from sqlalchemy.orm import create_session
from test.fixture.base import Base
from test.fixture.pathed import Pathed
def readurls():
"""read URLs from config file return a list"""
filename = 'test_db.cfg'
fullpath = os.path.join(os.curdir,filename)
ret=[]
ret = list()
# TODO: remove tmpfile since sqlite can store db in memory
tmpfile = Pathed.tmp()
fullpath = os.path.join(os.curdir, filename)
try:
fd = open(fullpath)
except IOError:
raise IOError("""You must specify the databases to use for testing!
Copy %(filename)s.tmpl to %(filename)s and edit your database URLs.""" % locals())
#fd = resource_stream('__main__',filename)
for line in fd:
if line.startswith('#'):
continue
@ -26,17 +34,18 @@ def readurls():
def is_supported(url, supported, not_supported):
db = url.split(':', 1)[0]
if supported is not None:
if isinstance(supported, basestring):
supported = (supported,)
ret = db in supported
return supported == db
else:
return db in supported
elif not_supported is not None:
if isinstance(not_supported, basestring):
not_supported = (not_supported,)
ret = not (db in not_supported)
return not_supported != db
else:
ret = True
return ret
return not (db in not_supported)
return True
# we make the engines global, which should make the tests run a bit faster
urls = readurls()
@ -54,10 +63,10 @@ def usedb(supported=None,not_supported=None):
to be supported
"""
if supported is not None and not_supported is not None:
msg = "Can't specify both supported and not_supported in fixture.db()"
assert False, msg
raise AssertionError("Can't specify both supported and not_supported in fixture.db()")
my_urls = [url for url in urls if is_supported(url, supported, not_supported)]
def dec(func):
def entangle(self):
for url in my_urls:
@ -77,14 +86,6 @@ class DB(Base):
level = TXN
def shortDescription(self,*p,**k):
"""List database connection info with description of the test"""
ret = super(DB,self).shortDescription(*p,**k) or str(self)
engine = self._engineInfo()
if engine is not None:
ret = "(%s) %s"%(engine,ret)
return ret
def _engineInfo(self,url=None):
if url is None:
url = self.url

View File

@ -51,7 +51,7 @@ class TestRepository(fixture.Pathed):
class TestVersionedRepository(fixture.Pathed):
"""Tests on an existing repository with a single python script"""
script_cls = script.PythonScript
def setup(self):
def setUp(self):
Repository.clear()
self.path_repos=self.tmp_repos()
# Create repository, script