You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Starting in the Functions chapter, we have written unit tests using the <c>testEqual</c> function from
4
+
the <c>test</c> module. This test module is not included in the standard Python distribution. (There is
5
+
a standard test module but it is different from this.) What follows is the source code for this <c>test</c> module.</p>
6
+
<program language="python"><input>
7
+
def testEqual(actual,expected,places=5):
8
+
'''
9
+
Does the actual value equal the expected value?
10
+
For floats, places indicates how many places, right of the decimal, must be correct
11
+
'''
12
+
if isinstance(expected,float):
13
+
if abs(actual-expected) < 10**(-places):
14
+
print('\tPass')
15
+
return True
16
+
else:
17
+
if actual == expected:
18
+
print('\tPass')
19
+
return True
20
+
print('\tTest Failed: expected {} but got {}'.format(expected,actual))
21
+
return False
22
+
</input></program>
23
+
<p>To use this module when programming on your own computer, save the above code with the name <term>test.py</term> in the same folder as the python program you want to test.</p>
Copy file name to clipboardExpand all lines: pretext/Debugging/HowtoAvoidDebugging.ptx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,7 @@ print(final_time)
66
66
</input>
67
67
</program>
68
68
<p>Hmm, when you run this example you see that something unexpected has happened. You would not realize this was an error unless you first knew what the program was supposed to do.</p>
69
-
<exercisexml:id="db_q_ex3_1">
69
+
<exerciselabel="db_q_ex3_1">
70
70
<statement>
71
71
<p>Which of the following best describes what is wrong with the previous example?</p>
0 commit comments