[svn r42188] Revive test/compat.py again, seems to work fine for me. Can be used by patching sys.modules["unittest"] in the conftest.py file.

--HG--
branch : trunk
This commit is contained in:
xoraxax 2007-04-20 00:39:27 +02:00
parent 8f58035cde
commit 2dd380575c
1 changed files with 59 additions and 0 deletions

59
py/test/compat.py Normal file
View File

@ -0,0 +1,59 @@
import py
from py.__.test.outcome import Failed, Passed
class TestCaseUnit(py.test.collect.Function):
""" compatibility Unit executor for TestCase methods
honouring setUp and tearDown semantics.
"""
def execute(self, session):
boundmethod = self.obj
instance = boundmethod.im_self
instance.setUp()
try:
boundmethod()
finally:
instance.tearDown()
return Passed()
class TestCase(object):
"""compatibility class of unittest's TestCase. """
Function = TestCaseUnit
def setUp(self):
pass
def tearDown(self):
pass
def fail(self, msg=None):
""" fail immediate with given message. """
raise Failed(msg=msg)
def assertRaises(self, excclass, func, *args, **kwargs):
py.test.raises(excclass, func, *args, **kwargs)
failUnlessRaises = assertRaises
# dynamically construct (redundant) methods
aliasmap = [
('x', 'not x', 'assert_, failUnless'),
('x', 'x', 'failIf'),
('x,y', 'x!=y', 'failUnlessEqual,assertEqual, assertEquals'),
('x,y', 'x==y', 'failIfEqual,assertNotEqual, assertNotEquals'),
]
items = []
for sig, expr, names in aliasmap:
names = map(str.strip, names.split(','))
sigsubst = expr.replace('y', '%s').replace('x', '%s')
for name in names:
items.append("""
def %(name)s(self, %(sig)s, msg=""):
__tracebackhide__ = True
if %(expr)s:
raise Failed(msg=msg + (%(sigsubst)r %% (%(sig)s)))
""" % locals() )
source = "".join(items)
exec py.code.Source(source).compile()
__all__ = ['TestCase']