diff --git a/src/lib/unittest.py b/src/lib/unittest.py index 8cb8bfbb8c..9b8f544b71 100644 --- a/src/lib/unittest.py +++ b/src/lib/unittest.py @@ -114,7 +114,6 @@ class TestCase: def assertRaises(self, exc, func=None, *args, **kwargs): if func is None: return AssertRaisesContext(exc) - try: func(*args, **kwargs) assert False, "%r not raised" % exc @@ -124,7 +123,6 @@ class TestCase: raise - def skip(msg): def _decor(fun): # We just replace original fun with _inner @@ -141,19 +139,25 @@ def skipUnless(cond, msg): 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 @@ -163,7 +167,7 @@ class TestResult: def wasSuccessful(self): return self.errorsNum == 0 and self.failuresNum == 0 -# TODO: Uncompliant + def run_class(c, test_result): o = c() set_up = getattr(o, "setUp", lambda: None) @@ -171,7 +175,7 @@ def run_class(c, test_result): print('class', c.__qualname__) for name in dir(o): if name.startswith("test"): - print(name, end=' ...') + print(' ', name, end=' ...') m = getattr(o, name) try: set_up() diff --git a/src/tests/test_crypto_random.py b/src/tests/test_crypto_random.py index c8f89899cf..ecb3f95760 100644 --- a/src/tests/test_crypto_random.py +++ b/src/tests/test_crypto_random.py @@ -16,8 +16,7 @@ class TestCryptoRandom(unittest.TestCase): r = random.uniform(15) c[r] += 1 for i in range(15): - self.assertTrue(c[r] > 900) - self.assertTrue(c[r] < 1100) + self.assertAlmostEqual(c[r], 1000, delta=150) def test_bytes_length(self): for l in range(1024 + 1): @@ -33,9 +32,7 @@ class TestCryptoRandom(unittest.TestCase): for h in hexlify(b): c[chr(h)] += 1 for h in '0123456789abcdef': - self.assertTrue(c[h] > 900) - self.assertTrue(c[h] < 1100) - + self.assertAlmostEqual(c[h], 1000, delta=150) def test_shuffle(self): for l in range(256 + 1):