2022-08-06 20:55:07 +08:00
|
|
|
import _unittest
|
2022-08-11 17:21:01 +08:00
|
|
|
from PikaStdData import String
|
2022-08-06 21:19:23 +08:00
|
|
|
|
|
|
|
|
2022-08-06 23:26:04 +08:00
|
|
|
class TestResult:
|
2022-08-06 20:55:07 +08:00
|
|
|
def __init__(self):
|
2022-08-06 23:26:04 +08:00
|
|
|
self.errorsNum = 0
|
|
|
|
self.failuresNum = 0
|
|
|
|
self.skippedNum = 0
|
|
|
|
self.testsRun = 0
|
|
|
|
self.errors = []
|
|
|
|
self.failures = []
|
|
|
|
self.skipped = []
|
|
|
|
self._newFailures = 0
|
2022-08-06 20:55:07 +08:00
|
|
|
|
2022-08-06 23:26:04 +08:00
|
|
|
def wasSuccessful(self):
|
|
|
|
return self.errorsNum == 0 and self.failuresNum == 0
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
# Format is compatible with CPython.
|
|
|
|
s = "<unittest.result.TestResult "
|
|
|
|
s += "run=%d, " % self.testsRun
|
|
|
|
s += "errors=%d, " % self.errorsNum
|
|
|
|
s += "failures=%d, " % self.failuresNum
|
|
|
|
s += "skipped=%d>" % self.skippedNum
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
class TestCase:
|
2022-08-06 21:19:23 +08:00
|
|
|
def assertEqual(self, x, y):
|
|
|
|
msg = "%r vs (expected) %r" % (x, y)
|
2022-08-09 18:36:43 +08:00
|
|
|
assert x == y, msg
|
2022-08-06 20:55:07 +08:00
|
|
|
|
2022-08-06 21:19:23 +08:00
|
|
|
def assertNotEqual(self, x, y):
|
|
|
|
msg = "%r not expected to be equal %r" % (x, y)
|
2022-08-09 18:36:43 +08:00
|
|
|
assert x != y, msg
|
2022-08-06 20:55:07 +08:00
|
|
|
|
2022-08-06 21:19:23 +08:00
|
|
|
def assertLessEqual(self, x, y):
|
|
|
|
msg = "%r is expected to be <= %r" % (x, y)
|
2022-08-09 18:36:43 +08:00
|
|
|
assert x <= y, msg
|
2022-08-06 20:55:07 +08:00
|
|
|
|
2022-08-06 21:19:23 +08:00
|
|
|
def assertGreaterEqual(self, x, y):
|
|
|
|
msg = "%r is expected to be >= %r" % (x, y)
|
2022-08-09 18:36:43 +08:00
|
|
|
assert x >= y, msg
|
2022-08-06 20:55:07 +08:00
|
|
|
|
2022-08-06 21:19:23 +08:00
|
|
|
def assertTrue(self, x):
|
|
|
|
msg = "Expected %r to be True" % x
|
2022-08-09 18:36:43 +08:00
|
|
|
assert x, msg
|
2022-08-06 20:55:07 +08:00
|
|
|
|
2022-08-06 21:19:23 +08:00
|
|
|
def assertFalse(self, x):
|
|
|
|
msg = "Expected %r to be False" % x
|
2022-08-09 18:36:43 +08:00
|
|
|
assert not x, msg
|
|
|
|
|
|
|
|
def assertIs(self, x, y):
|
|
|
|
msg = "%r is not %r" % (x, y)
|
|
|
|
assert x is y, msg
|
|
|
|
|
|
|
|
def assertIsNot(self, x, y):
|
|
|
|
msg = "%r is %r" % (x, y)
|
|
|
|
assert x is not y, msg
|
|
|
|
|
|
|
|
def assertIsNone(self, x):
|
|
|
|
msg = "%r is not None" % x
|
|
|
|
assert x is None, msg
|
|
|
|
|
|
|
|
def assertIsNotNone(self, x):
|
|
|
|
msg = "%r is None" % x
|
|
|
|
assert x is not None, msg
|
|
|
|
|
|
|
|
def assertIn(self, x, y):
|
|
|
|
msg = "Expected %r to be in %r" % (x, y)
|
|
|
|
assert x in y, msg
|
2022-08-06 20:55:07 +08:00
|
|
|
|
2022-08-11 17:21:01 +08:00
|
|
|
def run(self, result: TestResult, suite_name):
|
|
|
|
for name in dir(self):
|
|
|
|
if String(name).startswith("test"):
|
|
|
|
result.testsRun += 1
|
|
|
|
self.test_fn = getattr(self, name)
|
|
|
|
print("[ RUN ] %s.%s" % (suite_name, name))
|
|
|
|
self.test_fn()
|
2022-08-06 23:26:04 +08:00
|
|
|
|
2022-08-06 20:55:07 +08:00
|
|
|
|
|
|
|
class TestSuite:
|
2022-08-06 21:19:23 +08:00
|
|
|
def __init__(self, name):
|
2022-08-06 20:55:07 +08:00
|
|
|
self._tests = []
|
|
|
|
self.name = name
|
|
|
|
|
2022-08-06 23:26:04 +08:00
|
|
|
def addTest(self, case):
|
|
|
|
self._tests.append(case)
|
2022-08-06 20:55:07 +08:00
|
|
|
|
2022-08-11 17:21:01 +08:00
|
|
|
def run(self, result: TestResult):
|
2022-08-06 23:26:04 +08:00
|
|
|
for case in self._tests:
|
|
|
|
case.run(result, self.name)
|
2022-08-06 20:55:07 +08:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2022-08-06 23:26:04 +08:00
|
|
|
class TextTestRunner:
|
2022-08-06 20:55:07 +08:00
|
|
|
def run(self, suite: TestSuite):
|
|
|
|
res = TestResult()
|
2022-08-06 23:26:04 +08:00
|
|
|
_ = suite.run(res)
|
2022-08-06 20:55:07 +08:00
|
|
|
print("----------------------------------------------------------------------")
|
2022-08-11 17:21:01 +08:00
|
|
|
print("[----------] %d tests from %s\n" % res.testsRun, suite.name)
|
2022-08-06 20:55:07 +08:00
|
|
|
if res.failuresNum > 0 or res.errorsNum > 0:
|
2022-08-09 18:36:43 +08:00
|
|
|
s = "FAILED"
|
|
|
|
s += " (%d errors, %d failures)" % (res.errorsNum, res.failuresNum)
|
|
|
|
print(s)
|
2022-08-06 20:55:07 +08:00
|
|
|
else:
|
2022-08-11 17:21:01 +08:00
|
|
|
msg = "[==========]"
|
2022-08-06 20:55:07 +08:00
|
|
|
if res.skippedNum > 0:
|
|
|
|
msg += " (skipped=%d)" % res.skippedNum
|
|
|
|
print(msg)
|
|
|
|
|
|
|
|
return res
|