1 """Copied from https://raw.githubusercontent.com/micropython/micropython-lib/cfa1b9cce0c93a3115bbff3886c9bbcddd9e8047/unittest/unittest.py """
2 class SkipTest(Exception):
6 class AssertRaisesContext:
8 def __init__(self, exc):
14 def __exit__(self, exc_type, exc_value, tb):
16 assert False, "%r not raised" % self.expected
17 if issubclass(exc_type, self.expected):
24 def fail(self, msg=''):
27 def assertEqual(self, x, y, msg=''):
29 msg = "%r vs (expected) %r" % (x, y)
32 def assertNotEqual(self, x, y, msg=''):
34 msg = "%r not expected to be equal %r" % (x, y)
37 def assertAlmostEqual(self, x, y, places=None, msg='', delta=None):
40 if delta is not None and places is not None:
41 raise TypeError("specify delta or places not both")
44 if abs(x - y) <= delta:
47 msg = '%r != %r within %r delta' % (x, y, delta)
51 if round(abs(y-x), places) == 0:
54 msg = '%r != %r within %r places' % (x, y, places)
58 def assertNotAlmostEqual(self, x, y, places=None, msg='', delta=None):
59 if delta is not None and places is not None:
60 raise TypeError("specify delta or places not both")
63 if not (x == y) and abs(x - y) > delta:
66 msg = '%r == %r within %r delta' % (x, y, delta)
70 if not (x == y) and round(abs(y-x), places) != 0:
73 msg = '%r == %r within %r places' % (x, y, places)
77 def assertIs(self, x, y, msg=''):
79 msg = "%r is not %r" % (x, y)
82 def assertIsNot(self, x, y, msg=''):
84 msg = "%r is %r" % (x, y)
85 assert x is not y, msg
87 def assertIsNone(self, x, msg=''):
89 msg = "%r is not None" % x
92 def assertIsNotNone(self, x, msg=''):
94 msg = "%r is None" % x
95 assert x is not None, msg
97 def assertTrue(self, x, msg=''):
99 msg = "Expected %r to be True" % x
102 def assertFalse(self, x, msg=''):
104 msg = "Expected %r to be False" % x
107 def assertIn(self, x, y, msg=''):
109 msg = "Expected %r to be in %r" % (x, y)
112 def assertIsInstance(self, x, y, msg=''):
113 assert isinstance(x, y), msg
115 def assertRaises(self, exc, func=None, *args, **kwargs):
117 return AssertRaisesContext(exc)
120 func(*args, **kwargs)
121 assert False, "%r not raised" % exc
122 except Exception as e:
123 if isinstance(e, exc):
131 # We just replace original fun with _inner
137 def skipIf(cond, msg):
142 def skipUnless(cond, msg):
151 def addTest(self, cls):
152 self.tests.append(cls)
155 def run(self, suite):
157 for c in suite.tests:
160 print("Ran %d tests\n" % res.testsRun)
161 if res.failuresNum > 0 or res.errorsNum > 0:
162 print("FAILED (failures=%d, errors=%d)" % (res.failuresNum, res.errorsNum))
165 if res.skippedNum > 0:
166 msg += " (%d skipped)" % res.skippedNum
178 def wasSuccessful(self):
179 return self.errorsNum == 0 and self.failuresNum == 0
182 def run_class(c, test_result):
184 set_up = getattr(o, "setUp", lambda: None)
185 tear_down = getattr(o, "tearDown", lambda: None)
187 if name.startswith("test"):
188 print("%s (%s) ..." % (name, c.__qualname__), end="")
192 test_result.testsRun += 1
195 except SkipTest as e:
196 print(" skipped:", e.args[0])
197 test_result.skippedNum += 1
200 test_result.failuresNum += 1
201 # Uncomment to investigate failure in detail
208 def main(module="__main__"):
212 if isinstance(c, object) and isinstance(c, type) and issubclass(c, TestCase):
215 m = __import__(module, fromlist=['']) # changed to permit non-top-level testing modules
217 for c in test_cases(m):
219 runner = TestRunner()
220 result = runner.run(suite)