-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_testing.py
More file actions
40 lines (31 loc) · 1.57 KB
/
Copy pathunit_testing.py
File metadata and controls
40 lines (31 loc) · 1.57 KB
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
''' A unit testing framework for ECOR1051 '''
def check_equal(description: str, outcome, expected) -> None:
"""
Print a "passed" message if outcome and expected have same type and
are equal (as determined by the == operator); otherwise, print a
"fail" message.
Parameter "description" should provide information that will help us
interpret the test results; e.g., the call expression that yields
outcome.
Parameters "outcome" and "expected" are typically the actual value returned
by a call expression and the value we expect a correct implementation
of the function to return, respectively. Both parameters must have the same
type, which must be a type for which == is used to determine if two values
are equal. Don't use this function to check if floats, lists of floats,
tuples of floats, etc. are equal.
"""
outcome_type = type(outcome)
expected_type = type(expected)
if outcome_type != expected_type:
# The format methods is explained on pages 119-122 of
# 'Practical Programming', 3rd ed.
print("{0} FAILED: expected ({1}) has type {2}, " \
"but outcome ({3}) has type {4}".
format(description, expected, str(expected_type).strip('<class> '),
outcome, str(outcome_type).strip('<class> ')))
elif outcome != expected:
print("{0} FAILED: expected {1}, got {2}".
format(description, expected, outcome))
else:
print("{0} PASSED".format(description))
print("------")