-
Notifications
You must be signed in to change notification settings - Fork 13
/
Test.py
73 lines (59 loc) · 2.06 KB
/
Test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import atexit
from datetime import datetime
class Test(object):
"""
Implements the test interface as described here:
http://www.codewars.com/docs/python-test-reference-1
"""
def __init__(self):
self.desc = u"Undefined"
self.itmsg = u"Undefined"
self.failures = 0
self.successes = 0
self.start = datetime.now()
def describe(self, msg):
print msg
self.desc = msg
def it(self, msg):
print msg
self.itmsg = msg
def _assert(self, p, actual, expected, msg):
if not p(expected, actual):
self._error(msg, expected, actual)
else:
self._success()
def assert_equals(self, actual, expected, msg=u"{} should be {}"):
eq = lambda a, b: a == b
self._assert(eq, actual, expected, msg)
def assert_not_equals(self, actual, unexpected, msg=u"{} should be {}"):
neq = lambda a, b: a != b
self._assert(neq, actual, unexpected, msg)
def expect_error(self, msg, fn):
try:
fn()
self._error(u"Expected an error" if not msg else msg, None, None)
except:
self._success()
def expect(self, b, msg=u"Unexpected result"):
be = lambda a, e: b
self._assert(be, b, None, msg)
def _error(self, msg, expected, actual):
print u"*** ERROR: {}".format(msg.format(actual, expected))
self.failures += 1
def _success(self):
print "Test Passed"
self.successes += 1
def report(self):
end = datetime.now()
print u"\nTest run complete"
print u"Passed: {}".format(self.successes)
print u"Failed: {}".format(self.failures)
print u"Total: {}".format(self.successes + self.failures)
delta = end - self.start
print u"Process took {:,}ms to complete".format((delta.microseconds + 1000000 * delta.seconds) // 1000)
if self.failures == 0:
print u"Happy Days!"
else:
print u"Better luck next time!"
test = Test()
atexit.register(test.report)