2014-05-12 23:56:29 +00:00
|
|
|
class SkipTest(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-05-12 10:07:36 +00:00
|
|
|
class AssertRaisesContext:
|
|
|
|
|
|
|
|
def __init__(self, exc):
|
|
|
|
self.expected = exc
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, tb):
|
|
|
|
if exc_type is None:
|
|
|
|
assert False, "%r not raised" % exc
|
|
|
|
if issubclass(exc_type, self.expected):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-04-01 21:19:21 +00:00
|
|
|
class TestCase:
|
|
|
|
|
2014-05-09 20:20:06 +00:00
|
|
|
def fail(self, msg=''):
|
2014-04-01 21:19:21 +00:00
|
|
|
assert False, msg
|
|
|
|
|
2014-05-09 20:20:06 +00:00
|
|
|
def assertEqual(self, x, y, msg=''):
|
2014-05-12 00:04:36 +00:00
|
|
|
if not msg:
|
|
|
|
msg = "%r vs (expected) %r" % (x, y)
|
|
|
|
assert x == y, msg
|
|
|
|
|
|
|
|
def assertIs(self, x, y, msg=''):
|
|
|
|
if not msg:
|
|
|
|
msg = "%r is not %r" % (x, y)
|
|
|
|
assert x is y, msg
|
2014-04-01 21:19:21 +00:00
|
|
|
|
2014-05-09 20:20:06 +00:00
|
|
|
def assertTrue(self, x, msg=''):
|
|
|
|
assert x, msg
|
2014-04-01 21:19:21 +00:00
|
|
|
|
2014-05-09 20:20:06 +00:00
|
|
|
def assertIn(self, x, y, msg=''):
|
|
|
|
assert x in y, msg
|
2014-04-01 21:19:21 +00:00
|
|
|
|
2014-05-09 20:20:06 +00:00
|
|
|
def assertIsInstance(self, x, y, msg=''):
|
|
|
|
assert isinstance(x, y), msg
|
2014-04-01 21:19:21 +00:00
|
|
|
|
2015-05-12 10:07:36 +00:00
|
|
|
def assertRaises(self, exc, func=None, *args, **kwargs):
|
|
|
|
if func is None:
|
|
|
|
return AssertRaisesContext(exc)
|
|
|
|
|
2014-04-01 21:19:21 +00:00
|
|
|
try:
|
2014-05-11 18:26:11 +00:00
|
|
|
func(*args, **kwargs)
|
2014-04-01 21:19:21 +00:00
|
|
|
assert False, "%r not raised" % exc
|
|
|
|
except Exception as e:
|
|
|
|
if isinstance(e, exc):
|
|
|
|
return
|
|
|
|
raise
|
2014-04-04 09:09:18 +00:00
|
|
|
|
2014-12-14 23:37:45 +00:00
|
|
|
def assertFalse(self, x, msg=''):
|
|
|
|
assert not x, msg
|
|
|
|
|
2014-04-04 09:09:18 +00:00
|
|
|
|
2014-05-12 23:56:29 +00:00
|
|
|
def skip(msg):
|
|
|
|
def _decor(fun):
|
|
|
|
# We just replace original fun with _inner
|
|
|
|
def _inner(self):
|
|
|
|
raise SkipTest(msg)
|
|
|
|
return _inner
|
|
|
|
return _decor
|
|
|
|
|
|
|
|
|
|
|
|
def skipUnless(cond, msg):
|
|
|
|
if cond:
|
|
|
|
return lambda x: x
|
|
|
|
return skip(msg)
|
|
|
|
|
|
|
|
|
2015-05-12 22:35:09 +00:00
|
|
|
class TestSuite:
|
|
|
|
def __init__(self):
|
|
|
|
self.tests = []
|
|
|
|
def addTest(self, cls):
|
|
|
|
self.tests.append(cls)
|
|
|
|
|
|
|
|
class TestRunner:
|
|
|
|
def run(self, suite):
|
|
|
|
res = TestResult()
|
|
|
|
for c in suite.tests:
|
|
|
|
run_class(c, res)
|
|
|
|
return res
|
|
|
|
|
|
|
|
class TestResult:
|
|
|
|
def __init__(self):
|
|
|
|
self.errorsNum = 0
|
|
|
|
self.failuresNum = 0
|
|
|
|
self.skippedNum = 0
|
|
|
|
self.testsRun = 0
|
|
|
|
|
|
|
|
def wasSuccessful(self):
|
|
|
|
return self.errorsNum == 0 and self.failuresNum == 0
|
|
|
|
|
2014-05-09 20:20:31 +00:00
|
|
|
# TODO: Uncompliant
|
2015-05-12 22:35:09 +00:00
|
|
|
def run_class(c, test_result):
|
2014-05-09 20:20:31 +00:00
|
|
|
o = c()
|
2014-05-14 18:57:40 +00:00
|
|
|
set_up = getattr(o, "setUp", lambda: None)
|
|
|
|
tear_down = getattr(o, "tearDown", lambda: None)
|
2014-05-09 20:20:31 +00:00
|
|
|
for name in dir(o):
|
|
|
|
if name.startswith("test"):
|
|
|
|
m = getattr(o, name)
|
2014-05-12 23:56:29 +00:00
|
|
|
try:
|
2014-05-14 18:57:40 +00:00
|
|
|
set_up()
|
2015-05-12 22:35:09 +00:00
|
|
|
test_result.testsRun += 1
|
2014-05-12 23:56:29 +00:00
|
|
|
m()
|
2014-05-14 18:57:40 +00:00
|
|
|
tear_down()
|
2014-05-12 23:56:29 +00:00
|
|
|
print(name, "...ok")
|
|
|
|
except SkipTest as e:
|
|
|
|
print(name, "...skipped:", e.args[0])
|
2015-05-12 22:35:09 +00:00
|
|
|
test_result.skippedNum += 1
|
2014-05-09 20:20:31 +00:00
|
|
|
|
|
|
|
|
2014-04-04 18:41:33 +00:00
|
|
|
def main(module="__main__"):
|
2015-07-24 06:26:37 +00:00
|
|
|
def test_cases(m):
|
|
|
|
for tn in dir(m):
|
|
|
|
c = getattr(m, tn)
|
|
|
|
if isinstance(c, object) and isinstance(c, type) and issubclass(c, TestCase):
|
|
|
|
yield c
|
|
|
|
|
2014-04-04 18:41:33 +00:00
|
|
|
m = __import__(module)
|
2015-07-24 06:26:37 +00:00
|
|
|
suite = TestSuite()
|
|
|
|
for c in test_cases(m):
|
|
|
|
suite.addTest(c)
|
|
|
|
runner = TestRunner()
|
|
|
|
result = runner.run(suite)
|
|
|
|
msg = "Ran %d tests" % result.testsRun
|
|
|
|
if result.skippedNum > 0:
|
|
|
|
msg += " (%d skipped)" % result.skippedNum
|
|
|
|
print(msg)
|